JavaScript数组常用方法
let arr = [1, 2, 3, 4, 5]
let newArr = arr.map(x => x*2)
let arr = [1, 2, 3, 4, 5]
num.forEach(x => x*2)
let arr = [1, 2, 3, 4, 5]
const isBigEnough => value => value >= 2
let newArr = arr.filter(isBigEnough )
let arr = [1, 2, 3, 4, 5]
const isLessThan4 => value => value < 4
const isLessThan6 => value => value < 6
arr.every(isLessThan4 )
arr.every(isLessThan6 )
let arr= [1, 2, 3, 4, 5]
const isLessThan4 => value => value < 4
const isLessThan6 => value => value > 6
arr.some(isLessThan4 )
arr.some(isLessThan6 )
let arr = [1, 2, 3, 4, 5]
const add = (a, b) => a + b
let sum = arr.reduce(add)
let arr = [1,2,3,4,5]
arr.push(6)
let arr = [1, 2, 3, 4, 5]
arr.pop()
console.log(arr)
console.log(arr.length)
let arr = [1, 2, 3, 4, 5]
arr.shift()
console.log(arr)
console.log(arr.length)
const array1 = [1, 2, 3];
console.log(array1.unshift(4, 5));
console.log(array1);
-
Array.isArray() 解释:用于确定传递的值是否是一个 Array。
Array.isArray([1, 2, 3]);
Array.isArray({foo: 123});
Array.isArray("foobar");
Array.isArray(undefined);
const array1 = ['a', 'b', 'c'];
const array2 = ['d', 'e', 'f'];
const array3 = array1.concat(array2);
console.log(array3);
-
Array.toString() 解释:此方法将数组转化为字符串
const array1 = [1, 2, 'a', '1a'];
console.log(array1.toString());
const elements = ['Fire', 'Air', 'Water'];
console.log(elements.join());
console.log(elements.join(''));
console.log(elements.join('-'));
const months = ['Jan', 'March', 'April', 'June'];
months.splice(1, 0, 'Feb');
console.log(months);
months.splice(4, 1, 'May');
console.log(months);
|