注意:在复制数组时要判断是否要修改数组元素,如果要修改数组元素的话,最好是使用数组深拷贝处理,复制数组的原始值。
var arr1 = [1,2]
var arr2 = [3,4]
var arrConcat = arr1.concat(arr2);// [ 1, 2, 3, 4 ]
var arr2Concat = arr2.concat(arr1);// [ 3, 4, 1, 2 ]
var arr = [1, 2, 3, 4, 5];
console.log(arr.join('*'));// 1*2*3*4*5
var arr = [1,2,3,4];
var arrfilter = arr.filter((element)=>{
return element > 2;
})
console.log(arrfilter);// [ 3, 4 ]
const animals = ['ant', 'bison', 'camel', 'duck', 'elephant'];
console.log(animals.slice(2));// ["camel", "duck", "elephant"]
console.log(animals.slice(2, 4));// ["camel", "duck"]
console.log(animals.slice(-2));// ["duck", "elephant"]
console.log(animals.slice(2, -1));// ["camel", "duck"]
console.log(animals.slice());// ["ant", "bison", "camel", "duck", "elephant"]
const arr = [1, 2, 3, 4, 5];
let arr1 = arr.map((element) => {
return element + 10;
});
console.log(arr1);// [ 11, 12, 13, 14, 15 ]
const arr = [1, 2, 3, 4, 5];
arr.forEach((element) => {
console.log(element);
});
// 1
// 2
// 3
// 4
// 5
// 使用语法
reduce(callbackFn)
reduce(callbackFn, initialValue)
// 具体操作
var arr = [1, 2, 3, 4, 5];
console.log(arr.reduce((accumulator, currentValue) => accumulator + currentValue, 0));// 15
console.log(arr.reduce((accumulator, currentValue) => accumulator + currentValue));// 15
// 参数解释
//accumulator: 上一次调用 callbackFn 的结果
//currentValue:当前元素的值
//initialValue:初始化的值
const arr = [5, 12, 8, 130, 44];
const found = arr.find((element) => element < 100);
console.log(found);// 5
const found1 = arr.find((element) => element > 100);
console.log(found1);// 130
const arr = [5, 12, 8, 130, 44];
const found = arr.findIndex((element) => element < 100);
console.log(found);// 0
const found1 = arr.findIndex((element) => element > 100);
console.log(found1);// 3
// 使用语法
indexOf(searchElement)
indexOf(searchElement, fromIndex)
// 具体操作
const beasts = ['ant', 'bison', 'camel', 'duck', 'bison'];
console.log(beasts.indexOf('bison'));// 1
console.log(beasts.indexOf('bison', 2));// 4
// 使用语法
lastIndexOf(searchElement)
lastIndexOf(searchElement, fromIndex)
//具体操作
const animals = ['Dodo', 'Tiger', 'Penguin', 'Dodo'];
console.log(animals.lastIndexOf('Dodo'));// 3
console.log(animals.lastIndexOf('Dodo',2));// 0
var arr = [1, 2, [3, [4, [5]]]];
var arrflat = arr.flat();
console.log(arrflat); // [ 1, 2, 3, [ 4, [ 5 ] ] ]
var arrflatForNum = arr.flat(3);
console.log(arrflatForNum); //[ 1, 2, 3, 4, 5 ]
const arr1 = [1, [1, [1]], [1], 1];
const result = arr1.flatMap((num) => (num === 1 ? [1, 1] : 3));
console.log(result);// [ 1, 1, 3, 3, 1, 1 ]
var arr = [1, 2, 3];
var del = arr.pop();
console.log(arr); // [1, 2]
var arr = [1, 2, 3];
var newlen = arr.push(4, 5);
console.log(arr);//[ 1, 2, 3, 4, 5 ]
console.log(newlen);// 5
var arr = ["a","b", "c"];
var del = arr.shift();
console.log(arr); // ['b', "c"]
console.log(del); // a
var arr = ["c", "d"];
var newlen = arr.unshift("a", "b");
console.log(arr); //[ 'a', 'b', 'c', 'd' ]
console.log(newlen); // 4
var arr = [9, 8, 3, 6, 2, 0];
arr.sort((a,b)=>{
return a-b;
})
console.log(arr);// [0, 2, 3, 6, 8, 9]
var arr = [1, 2, 3, 4, 5, 6];
arr.reverse();
console.log(arr);//[ 6, 5, 4, 3, 2, 1 ]
0
)到终止索引(默认为?array.length
)内的全部元素。 // 使用语法
fill(value)
fill(value, start)
fill(value, start, end)
// 具体操作
const arr = [1, 2, 3, 4];
console.log(arr.fill(0, 2, 4));// [1, 2, 0, 0]
console.log(arr.fill(5, 1));// [1, 5, 5, 5]
console.log(arr.fill(6));// [6, 6, 6, 6]
// 使用语法
splice(start)
splice(start, deleteCount)
splice(start, deleteCount, item1)
splice(start, deleteCount, item1, item2, itemN)
// 具体操作
const months = ['Jan', 'March', 'April', 'June'];
console.log(months.splice(1, 0, 'Feb'));// []
console.log(months);// ["Jan", "Feb", "March", "April", "June"]
console.log(months.splice(4, 1, 'May'));// ["June"]