Array.prototype.keys()

Object.keys()可以在稀疏数组中筛选出数组中实际存在的key,keys()迭代器不会忽略属性不存在的key

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

 

posted @ 2024-03-18 19:01  宫羽宫羽  阅读(5)  评论(0编辑  收藏  举报