Array方法总结(一)

Array.prototype.at()

at(index)  传入一个整数值参数,返回该索引对应的元素。传入负整数从数组最后一个元素开始倒数。

const array = [10, 8, 2];
console.log(array.at(-2)); // 8

Array.prototype.concat()

合并两个或多个数组。返回一个新数组。

const array1 = [7, [1, 3]];
const array2 = ['d', 'a', 'n'];
const array3 = array1.concat(array2);
console.log(array3); // [7, [1, 3], 'd', 'a', 'n'];

array1[1].push(2001);
console.log(array3); // [7, [1, 3, 2001], 'd', 'a', 'n'];

Array.prototype.copyWithin()

复制数组的一部分到同一数组中的另一位置,并返回自身。改变原数组。

copyWithin(target, start, end) 从start开始到end(不包括end)的元素复制到target开始的位置,不改变原数组长度。

copyWithin(target, start, end)
const array1 = ['l', 'i' ,'c', 'i', 'a'];
console.log(array1.copyWithin(0, 3, 4)); // ['i', 'i' ,'c', 'i', 'a']

console.log([1, 2, 3, 4, 5].copyWithin(-2)); // [1, 2, 3, 1, 2];
console.log([1, 2, 3, 4, 5].copyWithin(0, 3)); // [4, 5, 3, 4, 5];
console.log([1, 2, 3, 4, 5].copyWithin(0, 3, 4)); // [4, 2, 3, 4, 5];
console.log([1, 2, 3, 4, 5].copyWithin(-2, -3, -1)); // [1, 2, 3, 3, 4];

Array.prototype.entries()

返回一个新的数组迭代器对象,该对象包含数组中每个索引的键/值对。

const array = ['d', 'a', 'n'];
const iterator1 = array.entries();
console.log(iterator1.next().value); // [0, 'd']

for(const [index, element] of array.entries()) {
    console.log(index, element);
}
// 0 'd'
// 1 'a'
// 2 'n'

Array.prototype.every()

一个数组内的所有元素是否都通过指定函数的测试。返回一个布尔值。

const isBelowThreshold = (currentValue) = > currentValue < 40;
const array = [1, 30, 39, 29, 10, 13];
console.log(array.every(isBelowThreshold)); // true

检查一个数组是否是另一个数组的子集

const isSubset = (array1, array2) = >
    array2.every((element) => array1.includes(element));
console.log(isSubset([1, 2, 3, 4, 5, 6, 7], [5, 6, 7])); // true
console.log(isSubset([1, 2, 3, 4, 5, 6, 7], [5, 8, 7])); // false

Array.prototype.fill()

用一个固定值填充一个数组从起始索引(默认为0)到终止索引(不包含end)的全部元素。返回修改后的数组。

fill(value, start, end)

const array = [1, 2, 3, 4];
console.log(array.fill(0, 2, 4)); // [1, 2, 0, 0]
console.log(array.fill(0, 1, 3)); // [1, 0, 0, 4]
console.log(array.fill(5, 1)); // [1, 5, 5, 5]
console.log(array.fill(6)); // [6, 6, 6, 6]

Array.prototype.filter()

返回一个新数组,包含所有通过所提供函数实现的测试的所有元素。

const words = ['spray', 'elite', 'exuberant', 'destruction', 'present'];

const result = words.filter((word) => word.length > 6);

console.log(result); // ['exuberant', 'destruction', 'present']

Array.prototype.find()

返回数组中满足条件的第一个元素的值。否则返回undefined。

const array = [5, 12, 8, 130, 44];
const found = array.find((element) => element > 10);
console.log(found); // 12

const inventory = [
    { name: "apples", quantity: 2 },
    { name: "bananas", quantity: 0 },
    { name: "cherries", quantity: 5 },
];
const result = inventory.find(({ name }) => name === "cherries");
console.log(result); // { name: 'cherries', quantity: 5 }

Array.prototype.findIndex()

返回满足条件的第一个元素的索引。没有找到对应元素返回-1。

const array = [5, 12, 8, 130, 44];
const isLargeNumber = (element) => element > 13;
console.log(array.findIndex(isLargeNumber)); // 3

Array.prototype.flat()

创建一个新的数组,将所有子数组元素拼接到新的数组中。

flat(depth)  指定要提取嵌套数组的结构深度,默认值为 1

const arr1 = [0, 1, 2, [3, 4]];
console.log(arr1.flat()); // [0, 1, 2, 3, 4]

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

console.log(arr2.flat()); // [0, 1, 2, [3, [4, 5]]]

console.log(arr2.flat(2)); // [0, 1, 2, 3, [4, 5]]

console.log(arr2.flat(Infinity)); // [0, 1, 2, 3, 4, 5]

从一组句子中生成单词列表。

const arr1 = ["it's Sunny in", "", "California"];

arr1.map((x) => x.split(" "));
// [["it's","Sunny","in"],[""],["California"]]

arr1.flatMap((x) => x.split(" "));
// ["it's","Sunny","in", "", "California"]

Array.prototype.forEach()

对数组的每个元素执行一次给定的函数。无返回值。

const array = ['a', 'b', 'c'];
array.forEach((element) => console.log(element));
// a
// b
// c

Array.from()

字符串转数组
console.log(Array.from('foo')); // ["f", "o", "o"]

用Set 或 Map创建数组
const set = new Set([1, 2, 3]);
const newArray = Array.from(set);
console.log(newArray); // [1, 2, 3]

对元素进行迭代处理
console.log(Array.from([1, 2, 3], (x) => x + x)); // [2, 4, 6]

const str = 'hello'
const strArr = Array.from(str,(s) =>s+'_')
console.log(strArr); // ["h_", "e_", "l_", "l_", "o_"]
console.log(strArr.join('')); // "h_e_l_l_o_"

数组、字符串的去重
const arr = [1, 2, 2, 3, 4, 4, 5];
const uniqueArray = Array.from(new Set(arr));
const uniqueArray2 =[...new Set(arr)]
const uniqueArray3 = _.uniq(arr) // 使用lodash中的方法    
console.log(uniqueArray); // [1, 2, 3, 4, 5]
console.log(uniqueArray2); // [1, 2, 3, 4, 5]
console.log(uniqueArray3); // [1, 2, 3, 4, 5]

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值