1、Array.from()
Used for array-like objects (that is length
, objects with attributes) and traversable objects into real arrays.
let json ={
'0':'11',
'1':'22',
'2':'33',
length:3
}
let arr = Array.from(json);
console.log(arr); // ["11", "22", "33"]
2、Array.of()
Convert a set of values into an array, the parameters are not divided into types, only the number, and the number is 0 to return an empty array.
let arr1 = Array.of(1,2,3);
let arr2 = Array.of([1,2,3]);
let arr3 = Array.of(undefined);
let arr4 = Array.of();
console.log(arr1); // [1, 2, 3]
console.log(arr2); // [[1, 2, 3]]
console.log(arr3); // [undefined]
console.log(arr4); // []
3、find()
find()
The method returns the value of the first element in the array that satisfies the provided test function. Otherwise, it returns undefined
. The parameter is the callback function.
let arr=[12,32,43,2,4,11,55,12];
let val=arr.find(function (ele,index) {
return ele>12;
});
console.log(val);//11
4、findIndex ()
findIndex
It's find
almost the same, but the index is returned by default. If there is no eligible element, it returns -1
let arr=[12,32,43,2,4,11,55,12];
let val=arr.findIndex(function (ele,index) {
return ele>32;
});
console.log(val);
5、fill()
fill()
Method is used to replace an element of the array with a fixed value.
var fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.fill("Runoob");//Runoob,Runoob,Runoob,Runoob
6. Traverse the array method keys(), values(), entries()
These three methods all return a traverser object, which can be for...of
traversed in a loop. The only difference keys()
is the traversal of the key name, the traversal of values()
the key value, and the traversal entries()
of the key-value pair.
keys()
let arr = ["a","b","c","d"];
for(let i of arr.keys()){
console.log(i);
}
//打印:
// 0
// 1
// 2
// 3
values()
let arr = ["a","b","c","d"];
for(let i of arr.values()){
console.log(i);
}
//打印:
// a
// b
// c
entries()
let arr = ["a","b","c","d"];
for(let i of arr.entries()){
console.log(i);
}
//打印:
// [0, "a"]
// [1, "b"]
// [2, "c"]
// [3, "d"]
for(let [idx,item] of arr.entries()){
console.log(idx+":"+item);
}
//打印:
// 0:a
// 1:b
// 2:c
7、includes()
includes()
The method is used to determine whether an array contains a specified value, if it is returned true
, otherwise false
.
[1, 2, 3].includes(2); // true
[1, 2, 3].includes(4); // false
[1, 2, 3].includes(3, 3); // false
[1, 2, 3].includes(3, -1); // true
[1, 2, NaN].includes(NaN); // true
8、copyWithin()
copyWithin()
The method is used to copy elements from a specified position in the array to another specified position in the array. Will overwrite the original members.
var fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.copyWithin(2, 0);//Banana,Orange,Banana,Orange