let arr = [1,2,3,4,5] console.log(arr.slice()); // [ 1, 2, 3, 4, 5 ] console.log(arr.slice(1)); // [ 2, 3, 4, 5 ] console.log(arr.slice(2,4)); // [ 3, 4 ] console.log(arr); // [1,2,3,4,5]
let arr = [1,2,3,4,5]; console.log(arr.splice(2), arr); // [ 3, 4, 5 ] [ 1, 2 ] console.log(arr.splice(0,1,2), arr); // [ 1 ] [ 2, 2, 3, 4, 5 ]
let arr = [1,2,3], arr1 = [4]; console.log(arr.concat(arr1, 5), arr); // [ 1, 2, 3, 4, 5, ] [ 1, 2, 3 ] console.log([...arr, ...arr1, 5]); // [ 1, 2, 3, 4, 5 ] (對象也能夠這樣拼接,但重複的會覆蓋,至關與 Object.assign)
Array.of(1,2,3,4); // [ 1, 2, 3, 4 ] Array.from({0: 'zero', 1: 'one', length: 2}); // [ 'zero', 'one' ]
typeof操做符能夠判斷出number、boolean、string、function和undefined,而不能判斷處具體的Object類型。數組
!!myVar
arr = [1, '2', null, NaN]; arr.indexOf(NaN); // -1 arr.includes(NaN); // true
// let arr = [1, '2', null, NaN]; // arr.filter(item => typeof item === 'number'); // [1, NaN] // arr.find(item => typeof item === 'number'); // 1 // arr.findIndex(item => typeof item === 'number'); // 0
let arr = [Array(), [], {}, null] let res1 = arr.some((item, index, arr) => { return Object.prototype.toString.call(item) === '[object Array]' }) let res2 = arr.every((item, index, arr) => { return Object.prototype.toString.call(item) === '[object Array]' }) console.log(res1,res2); // true false
let res = [0,1,2,3,4].map((item, idx)=> item * idx); // [ 0, 1, 4, 9, 16 ]
console.log( [1,2,3].reduce((a,b,c,d)=>{ console.log(a,b,c,d); return a+b; // 下一次的a }, 4) ); // 10 /* 4 1 0 [ 1, 2, 3 ] 5 2 1 [ 1, 2, 3 ] 7 3 2 [ 1, 2, 3 ] */
固然累乘或者作其餘業務也是能夠的。函數