Array方法总结(二)

Array.prototype.includes()

判断一个数组是否包含一个指定的值,包含返回true,否则返回false。

includes(searchElement, fromIndex)  开始搜索的索引(从零开始)。

如果 fromIndex大于等于数组的长度,则将直接返回 false,且不搜索该数组。

const array = ['dan', 'licia', 'larea'];
console.log(array.includes('larea')); // true

[1, 2, 3].includes(3, -1); // true
[1, 2, NaN].includes(NaN); // true
["1", "2", "3"].includes(3); // false

Array.prototype.indexOf()

返回数组中第一次出现给定元素的下标,如果不存在则返回-1。

indexOf(searchElement, fromIndex)。

没法使用 indexOf() 来搜索 NaN

const beats = ['ant', 'bison', 'camel'];
console.log(beats.indexOf('bison')); // 1
console.log(beats.indexOf('bison', 2)); // -1

找出指定元素出现的所有位置
const indices = [];
const array = ["a", "b", "a", "c", "a", "d"];
const element = "a";
let idx = array.indexOf(element);
while (idx !== -1) {
  indices.push(idx);
  idx = array.indexOf(element, idx + 1);
}
console.log(indices); // [0, 2, 4]

Array.isArray()

判断传递的值是否是一个数组。

console.log(Array.isArray[1, 3, 5]); // true
console.log(Array.isArray('[]')); // false

Array.prototype.join()

将一个数组的所有元素连接成一个字符串并返回这个字符串。

console.log([1, , 3].join()); // '1,,3'
console.log([1, undefined, 3].join()); // '1,,3'

Array.prototype.keys()

const array1 = ['a', 'b', 'c'];
const iterator = array1.keys();

for (const key of array1) {
  console.log(key);
}
// 'a'
// 'b'
// 'c'

for (const key of iterator) {
  console.log(key);
}
// 0
// 1
// 2

 Object.keys() 只包含数组中实际存在的键,keys() 迭代器不会忽略缺失属性的键。

const arr = ["a", , "c"];
const sparseKeys = Object.keys(arr);
const denseKeys = [...arr.keys()];
console.log(sparseKeys); // ['0', '2']
console.log(denseKeys); // [0, 1, 2]

Array.prototype.values()

返回一个新的数组迭代器对象,该对象迭代数组中每个元素的值。

const array1 = ['a', 'b', 'c'];
const iterator = array1.values();

for (const value of iterator) {
  console.log(value);
}

// "a"
// "b"
// "c"

Array.prototype.lastIndexOf()

lastIndexOf(searchElement, fromIndex)

const numbers = [2, 5, 9, 2];
numbers.lastIndexOf(2); // 3
numbers.lastIndexOf(7); // -1
numbers.lastIndexOf(2, 3); // 3
numbers.lastIndexOf(2, 2); // 0
numbers.lastIndexOf(2, -2); // 0
numbers.lastIndexOf(2, -1); // 3

不能用 lastIndexOf() 来搜索 NaN

const array = [NaN];
array.lastIndexOf(NaN); // -1

Array.prototype.map()

创建一个新数组,新数组由原数组中的每个元素都调用一次提供的函数后的返回值组成。

使用map重新格式化数组中的对象。

const kvArray = [
  { key: 1, value: 10 },
  { key: 2, value: 20 },
  { key: 3, value: 30 },
];

const reformattedArray = kvArray.map(({ key, value }) => ({ [key]: value }));

console.log(reformattedArray); // [{ 1: 10 }, { 2: 20 }, { 3: 30 }]
console.log(kvArray);
// [
//   { key: 1, value: 10 },
//   { key: 2, value: 20 },
//   { key: 3, value: 30 }
// ]

Array.of()

通过参数创建一个新的Array实例,不考虑参数的数量或类型。

console.log(Array.of('foo', 2, 'bar', true)); // ["foo", 2, "bar", true]
console.log(Array.of()); // []

console.log(Array.of(7)); // [7]
console.log(Array(7)); // [undefined, undefined, undefined, undefined, undefined, undefined, undefined]

console.log(Array.of(1, 2, 3)); // [1, 2, 3]
console.log(Array(1, 2, 3)); // [1, 2, 3]

Array.prototype.pop()

从数组中删除最后一个元素,并返回该元素的值。

const plan = ['banana', 'apple', 'strawberry'];
console.log(plan.pop()); // "strawberry"
console.log(plan); // ['banana', 'apple']

Array.prototype.push()

将指定的元素添加到数组的末尾,并返回新的数组长度。

const animals = ['pig', 'monkey', 'snake'];
const count = animals.push('cows');
console.log(count); // 4
console.log(animals); // ['pig', 'monkey', 'snake', 'cows']

合并两个数组。

const vegetables = ["parsnip", "potato"];
const moreVegs = ["celery", "beetroot"];

// 合并第二个数组到第一个数组中
vegetables.push(...moreVegs);

console.log(vegetables); // ['parsnip', 'potato', 'celery', 'beetroot']

Array.prototype.reduce()

对数组中的每个元素按序执行一个提供的reducer函数,每次运行会将先前元素的计算结果作为参数传入,最后返回结果。

如果有初始值,则回调函数从数组索引为0的元素开始执行,如果没有初始值,则将数组索引为0的元素作为初始值,回调函数从第二个元素开始执行。

const array1 = [1, 2, 3, 4];

// 0 + 1 + 2 + 3 + 4
const initialValue = 0;
const sumWithInitial = array1.reduce(
  (accumulator, currentValue) => accumulator + currentValue,
  initialValue,
);

console.log(sumWithInitial);
// Expected output: 10

Array.prototype.reduceRight()

对累加器(accumulator)和数组的每个值(按从右到左的顺序)应用一个函数,并使其成为单个值。

const array1 = [
  [0, 1],
  [2, 3],
  [4, 5],
];

const result = array1.reduceRight((accumulator, currentValue) =>
  accumulator.concat(currentValue),
);

console.log(result); // [4, 5, 2, 3, 0, 1]
[0, 1, 2, 3, 4].reduceRight(
  (accumulator, currentValue, index, array) => accumulator + currentValue,
);
accumulator currentValueindex返回值
第一次调用4337
第二次调用7229
第三次调用91110
第四次调用100010

Array.prototype.reverse()

就地反转数组中的元素,并返回同一数组的引用。改变原数组。

const numbers = [3, 2, 4, 1, 5];
const reversed = numbers.reverse();
// numbers 和 reversed 的顺序都是颠倒的 [5, 1, 4, 2, 3]
reversed[0] = 6;
console.log(numbers[0]); // 6

Array.prototype.toReversed()

返回一个元素顺序相反的新数组。

const items = [1, 2, 3];
console.log(items); // [1, 2, 3]

const reversedItems = items.toReversed();
console.log(reversedItems); // [3, 2, 1]
console.log(items); // [1, 2, 3]

console.log([1, , 3, 4].toReversed()); // [4, 3, undefined, 1]

Array.prototype.shift()

从数组中删除第一个元素,并返回该元素的值。

const array = [1, 2, 3];
const firstElement = array.shift();
console.log(array); // [2, 3]
console.log(firstElement); // 1

Array.prototype.unshift()

将指定元素添加到数组的开头,并返回数组的新长度。

const array1 = [1, 2, 3];
console.log(array1.unshift(4, 5)); // 5
console.log(array1); // [4, 5, 1, 2, 3]

  • 30
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值