数组 讲解之 扩展

2020-08-25  本文已影响0人  zhang463291046

以下内容是引用或者借鉴别人的,自己只是做个笔记,方便学习。理解错误的地方,欢迎评论。如有侵权,私聊我删除,未经允许,不准作为商业用途

静态方法

let arrayLike = {
    '0': 'a',
    '1': 'b',
    '2': 'c',
    length: 3
};
// ES5的写法
var arr1 = [].slice.call(arrayLike); // ['a', 'b', 'c']
// ES6的写法
let arr2 = Array.from(arrayLike); // ['a', 'b', 'c']

Array.from('hello')  // ['h', 'e', 'l', 'l', 'o']

let namesSet = new Set(['a', 'b'])
Array.from(namesSet) // ['a', 'b']

Array.from([1, 2, 3])  // [1, 2, 3]

Array.from({ length: 3 });  // [ undefined, undefined, undefined ]

// 第二个参数,对每个元素进行处理
Array.from(arrayLike, x => x * x);
// 等同于
Array.from(arrayLike).map(x => x * x);
Array.from([1, 2, 3], (x) => x * x)  // [1, 4, 9]
Array.from({ length: 2 }, () => 'jack')  // ['jack', 'jack']
Array.of(3, 11, 8) // [3,11,8]
Array.of(3) // [3]
Array.of(3).length // 1
Array.of() // []
Array.of(undefined) // [undefined]

静态方法图解

image.png

实例方法

// copyWithin(target, start = 0, end = this.length)
[1, 2, 3, 4, 5].copyWithin(0, 3)  // [4, 5, 3, 4, 5]

// 将3号位复制到 0号位
[1, 2, 3, 4, 5].copyWithin(0, 3, 4)  // [4, 2, 3, 4, 5]

// -2相当于3号位,-1相当于4号位
[1, 2, 3, 4, 5].copyWithin(0, -2, -1)  // [4, 2, 3, 4, 5]

// 将3号位复制到 0号位
[].copyWithin.call({length: 5, 3: 1}, 0, 3)// {0: 1, 3: 1, length: 5}

//  第一个参数是回调函数可以接受三个参数,依次为当前的值、当前的位置和原数组
[1, 4, -5, 10].find((n) => n < 0)  // -5
[1, 4, -5, 10].find((n) => n < -10)  // undefined
[1, 5, 10, 15].find(function(value, index, arr) {
  return value > 9;
}) // 10

// 第二个参数,用来绑定回调函数的this对象
function f(v){
  return v > this.age;
}
let person = {name: 'John', age: 20};
[10, 12, 26, 15].find(f, person);    // 26
[1, 4, -5, 10].findIndex((n) => n < 0)  // 2
[1, 4, -5, 10].findIndex((n) => n < -10)  // -1
[1, 5, 10, 15].findIndex(function(value, index, arr) {
  return value > 9;
}) // 2

[NaN].findIndex(y => Object.is(NaN, y))
// 0
['a', 'b', 'c'].fill(7)
// [7, 7, 7]

new Array(3).fill(7)
// [7, 7, 7]

// 接受第二个和第三个参数,用于指定填充的起始位置和结束位置
['a', 'b', 'c'].fill(7, 1, 2)  // ['a', 7, 'c']

// 填充的类型为对象,那么被赋值的是同一个内存地址的对象,而不是深拷贝对象
let arr = new Array(3).fill({name: "Mike"});
arr[0].name = "Ben";
arr  // [{name: "Ben"}, {name: "Ben"}, {name: "Ben"}]
for (let index of ['a', 'b'].keys()) {
  console.log(index);
}
// 0
// 1

for (let elem of ['a', 'b'].values()) {
  console.log(elem);
}
// 'a'
// 'b'

for (let [index, elem] of ['a', 'b'].entries()) {
  console.log(index, elem);
}
// 0 "a"
// 1 "b"
[1, 2, 3].includes(2)     // true
[1, 2, 3].includes(4)     // false
[1, 2, NaN].includes(NaN) // true
//第二个参数表示搜索的起始位置
[1, 2, 3].includes(3, 3);  // false
[1, 2, 3].includes(3, -1); // true
[1, 2, [3, 4]].flat()  // [1, 2, 3, 4]
[1, 2, [3, 4]].flat(1)  // [1, 2, 3, 4]
[1, 2, [3, [4, 5]]].flat()  // [1, 2, 3, [4, 5]]
[1, 2, [3, [4, 5]]].flat(2)  // [1, 2, 3, 4, 5]
[1, [2, [3]]].flat(Infinity)  // [1, 2, 3]
[1, 2, , 4, 5].flat()  // [1, 2, 4, 5]
// 相当于 [[2, 4], [3, 6], [4, 8]].flat()
[2, 3, 4].flatMap((x) => [x, x * 2])
// [2, 4, 3, 6, 4, 8]
//按照字符编码的顺序进行排序,数组的元素转换成字符串,再进行比较
[25, 1, 4, -5, 10].sort()  //[-5, 1, 10, 25, 4]

//升序 : a大于b,返回大于零的数;a小于b,返回小于零的数;
[25, 1, 4, -5, 10].sort((a,b)=>a-b)  // [-5, 1, 4, 10, 25]
[25, 1, 4, -5, 10].sort((a,b)=>{
  return a - b > 0 ? 1 : -1;
}) 
// [-5, 1, 4, 10, 25]
[{ name: "name1", age: 25 },{ name: "name2", age: 1 },{ name: "name3", age: 4 },{ name: "name4", age: -5 },{ name: "name5", age: 10 }].sort((a, b) => {
  return a.age - b.age > 0 ? 1 : -1;
});
//[{name: "name4", age: -5}, {name: "name2", age: 1}, {name: "name3", age: 4},{name: "name5", age: 10},{name: "name1", age: 25}]

实例方法图解

image.png

扩展运算符...

console.log(...[1, 2, 3])
// 1 2 3
console.log(1, ...[2, 3, 4], 5)
// 1 2 3 4 5
console.log([1, ...[2, 3, 4], 5])
//[1, 2, 3, 4, 5]
[...document.querySelectorAll('div')]
// [<div>, <div>, <div>]

//替代函数的 apply 方法
// ES5 的写法
Math.max.apply(null, [14, 3, 77])
// ES6 的写法
Math.max(...[14, 3, 77])
// 等同于
Math.max(14, 3, 77);

// 复制数组
const a1 = [1, 2];
const a2 = [...a1];

//  合并数组
const arr1 = ['a', 'b'];
const arr2 = ['c'];
const arr3 = ['d', 'e'];
// ES5 的合并数组
arr1.concat(arr2, arr3);
// [ 'a', 'b', 'c', 'd', 'e' ]
// ES6 的合并数组
[...arr1, ...arr2, ...arr3]
// [ 'a', 'b', 'c', 'd', 'e' ]

//解构赋值
const [first, ...rest] = [1, 2, 3, 4, 5];
first // 1
rest  // [2, 3, 4, 5]

// 字符串
[...'hello']  // [ "h", "e", "l", "l", "o" ]

数组的空位 empty

[1,undefined,null,"",,2] //[1, undefined, null, "", empty, 2]
0 in [undefined, undefined, undefined] // true
0 in [, , ,] // false
上一篇下一篇

猜你喜欢

热点阅读