ES6数组算法总结 前端面试题数组算法考点 (部分)

1.数组去重 new Set()

var set = new Set([1,2,3,4,4,8,null,null,Array,Object,Object]);
console.log([...set]);

在这里插入图片描述
2.类似数组或可迭代对象创建一个新的,浅拷贝的数组 Array.from()
注意:创造出新的

console.log(Array.from('foo'));
console.log(Array.from([1, 2, 3], x => x + x));

Array [“f”, “o”, “o”]
Array [2, 4, 6]

3.判断是否为数组 Array.isArray(obj)
若obj为数组 则输出ture 否则输出false
例如:

Array.isArray([1, 2, 3]);
Array.isArray({foo: 123});

true
false

4.数组拼接合并 Array.concat()
注意:此方法不会更改现有数组,而是返回一个新数组

const array1 = ['a', 'b', 'c'];
const array2 = ['d', 'e', 'f'];
const array3 = array1.concat(array2);

Array [“a”, “b”, “c”, “d”, “e”, “f”]

5.检测数组所有元素是否都满足条件 arr.every()
注意 :若检测的arr数组为空时,在任何情况下它返回的都是true

const isBelowThreshold = (currentValue) => currentValue < 40;  //需要满足的条件
const array1 = [1, 30, 39, 29, 10, 13];
console.log(array1.every(isBelowThreshold));

true

6.检测数组是否至少有1个元素满足条件 arr.some()
注意 :若检测的arr数组为空时,在任何情况下它返回的都是false

const isBelowThreshold = (currentValue) => currentValue < 10;  //需要满足的条件
const array1 = [1, 30, 39, 29, 10, 13];
console.log(array1.some(isBelowThreshold));

true

7.数组排序 arr.sort(function)
function<object<null<Array<(string与number进行比较按转换为数字后的大小进行排序)<undefined

var arr=[1,41,10,5,6,"7",true,null,undefined,[],{},function(){}]
var b=arr.sort((a,b)=>{
  return a-b
})
console.log(b)

Array [function(){}, Object { }, null, Array [], 1, true, 5, 6, “7”, 10, 41, undefined]

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值