Array.from()
The array. from method is used to convert two types of objects into true arrays: array-like objects and iterable objects (including the new ES6 data structures Set and Map)
let arr2 = Array.from(arrayLike);
Copy the code
Array.of()
The getValue () method converts a set of values to an array
Array.of(3.11.8) / /,11,8 [3]
Array.of(3) / / [3]
Array.of(3).length / / 1
Copy the code
Array. The find and findIndex
Find returns the number, and findIndex returns the corner index
[1.5.10.15].find(function(value, index, arr) {
return value > 9;
})
Copy the code
Array.fill()
The fill method fills an array with the given value.
['a'.'b'.'c'].fill(7)
/ / (7, 7, 7)
new Array(3).fill(7)
/ / (7, 7, 7)
Copy the code
Array.includes()
[1.2.3].includes(2); // true
[1.2.3].includes(4); // false
[1.2.NaN].includes(NaN); // true
Copy the code
Array.filter every some map foreach
Item, index, arr forEach: no return value, undefined if returned. Arr forEach: no return value, undefined if returned. There are three arguments item, index, and arr every: the return value is a Boolean value. True is returned only if all elements have a certain property. Return false if either item is not satisfied. Some: The return value is a Boolean true. As long as one of these values is true, the loop ends, and the following data does not continue to judge the value of the filter: filter array. The result of the condition is true.Copy the code
let arr = ['c'.'d'.'e'];
arr.forEach((item) = >{
item = 'a';
});
console.log(arr);// The array does not change
let arrMap = arr.map((item) = >{
return item = 'a';
});
console.log(arrMap);// return ["a", "a", "a"]
let flag = arr.every((item) = >{
return item==='a'
});
console.log(flag);//false
flag = arrMap.every((item) = >{
return item==='a'
});
console.log(flag);//true
flag = arr.some((item) = >{
return item==='c'
});
console.log(flag);//true
flag = arrMap.some((item) = >{
return item==='a'
});
console.log(flag);//true
Copy the code
About the vacancy of the array
ES5’s treatment of empty seats is already quite inconsistent, mostly ignoring empty seats.
forEach()
.filter()
.every()
和some()
All jump over the empty space.map()
The void is skipped, but the value is preservedjoin()
andtoString()
Will treat the empty space asundefined
And theundefined
andnull
Will be processed as an empty string
The following
/ / the forEach method
[,'a'].forEach((x,i) = > console.log(i)); / / 1
/ / filter method
['a'.'b'].filter(x= > true) // ['a','b']
/ / every method
[,'a'].every(x= > x==='a') // true
/ / some methods
[,'a'].some(x= >x ! = ='a') // false
/ / the map method
[,'a'].map(x= > 1) // [,1]
/ / join method
[,'a'.undefined.null].join(The '#') // "#a##"
/ / the toString method
[,'a'.undefined.null].toString() // ",a,,"
Copy the code
ES6 explicitly converts empty space to undefined
The array. from method converts the empty space of the Array to undefined, which means it doesn’t ignore the empty space.
Array.from(['a'.'b'])
// [ "a", undefined, "b" ]
Copy the code
Extended operators (…) It also changes the empty space to undefined.
[[...'a'.'b']]// [ "a", undefined, "b" ]
Copy the code
CopyWithin () copies the empty space.
[,'a'.'b',,].copyWithin(2.0) // [,"a",,"a"]
Copy the code
Fill () treats the empty space as a normal array position.
new Array(3).fill('a') // ["a","a","a"]
Copy the code
for… The of loop also iterates over the empty space.
let arr = [, ,];
for (let i of arr) {
console.log(1);
}
/ / 1
/ / 1
Copy the code
Entries (), keys(), values(), find() and findIndex() treat empty Spaces as undefined.
// entries()[[,...'a'].entries()] // [[0,undefined], [1,"a"]]
// keys()[[,...'a'].keys()] / / [0, 1]
// values()[[,...'a'].values()] // [undefined,"a"]
// find()
[,'a'].find(x= > true) // undefined
// findIndex()
[,'a'].findIndex(x= > true) / / 0
Copy the code