- concat() 连接两个或更多的数组,并返回结果。
let array1 = [1, 2, 3];
let array2 = [4, 5, 6];
let concatenatedArray = array1.concat(array2);
console.log(concatenatedArray);
- join() 把数组的所有元素放入一个字符串。元素通过指定的分隔符进行分隔。
let array = ['apple', 'banana', 'cherry'];
let joinedString = array.join(', ');
console.log(joinedString);
let array = [1, 2, 3];
let lastElement = array.pop();
console.log(array);
console.log(lastElement);
let array = [1, 2, 3];
let firstElement = array.shift();
console.log(array);
console.log(firstElement);
- push() 向数组的末尾添加一个或更多元素,并返回新的长度。
let array = [1, 2, 3];
array.push(4);
console.log(array);
array.push(5, 6);
console.log(array);
- unshift() 向数组的开头添加一个或更多元素,并返回新的长度。
let array = [1, 2, 3];
array.unshift(0);
console.log(array);
array.unshift(-2, -1);
console.log(array);
let array = [1, 2, 3, 4, 5];
array.reverse();
console.log(array);
- reduce() 对数组中的每个元素执行一个由您提供的reducer函数(升序执行),将其结果汇总为单个返回值
let array = [1, 2, 3, 4, 5];
let sum = array.reduce((accumulator, currentValue) => accumulator + currentValue, 0);
console.log(sum);
let array = [5, 2, 9, 1, 5, 6];
array.sort((a, b) => a - b);
console.log(array);
let array = [1, 2, 3, 4, 5];
let slicedArray = array.slice(1, 3);
console.log(slicedArray);
let array = [1, 2, 3, 4, 5];
let removed = array.splice(2, 0, 'a', 'b');
console.log(array);
console.log(removed);
removed = array.splice(2, 2);
console.log(array);
console.log(removed);
- toString() 把数组转换为字符串,并返回结果。
let array = [1, 2, 3];
console.log(array.toString());
- toLocaleString() 把数组转换为本地数组,并返回结果。
let array = [123456.789, -123456.789];
console.log(array.toLocaleString());
let array = [1, 2, 3];
console.log(array.toSource());
let array = [1, 2, 3];
console.log(array.valueOf());