var total = 0;
[1, 2, 3].forEach(function (num) {
total += num;
});
复制代码
var total = [1, 2, 3].reduce(function (sum, current) {
return sum + current;
}, 0);
复制代码
// 默认是升序
[1, 2, 3, 4].sort((a, b) => a - b); // [1, 2, 3, 4]
// 降序
[1, 2, 3, 4].sort((a, b) => b - a); // [4, 3, 2, 1]
复制代码
参考文献
Math.max() // -Infinity,即 -∞
Math.max(Infinity, -Infinity) // Infinity,即 ∞
Math.max(...[1, 2, 3, 4]) // 4
Math.max.apply(this, [1, 2, 3, 4]) // 4
[1, 2, 3, 4].reduce( (prev, cur,curIndex,arr)=> {
return Math.max(prev,cur);
},0) // 4
复制代码
[7, 8, 9].includes(4) // false
复制代码
[7, 8, 9].indexOf(4) // -1 如果存在返回索引
[7, 8, 9].indexOf(9) // 2
复制代码
如果数组中无值返回undefined
[7, 8, 9].find((item) => item === 4) // undefined
[7, 8, 9].find((item) => item === 9) // 9
复制代码
如果数组中无值返回-1
[7, 8, 9].findIndex((item)=> item === 4) // -1
[7, 8, 9].findIndex((item)=> item === 9) // 2
复制代码
arr = ['aaa', 'bbbb', 'cccc', 'dddd']
arr.splice(3, 0, 3)
arr // ['aaa', 'bbbb', 'cccc', 3, 'dddd']
复制代码
[7,8,9,10,11,12].fill() // [undefined, undefined, undefined, undefined, undefined, undefined]
[7,8,9,10,11,12].fill(7) // [7, 7, 7, 7, 7, 7]
[7,8,9,10,11,12].fill(3, 2, 4) // [7, 8, 3, 3, 11, 12]
复制代码
[7, 8, 9].map((item, idx) => item % 2) // [1, 0, 1]
[7, 8, 9].map((item, idx) => idx) // [0, 1, 2]
复制代码
[1,2,3].every(item=>{return item>2}) //false
复制代码
[1,2,3].some(item=>{return item>2}) //true
