1.数组去重
利用 es6
的 Set
结构不接受重复值,可以这样写(仅限数字):
[...new Set([1,2,3,1,'a',1,'a'])]
// [1, 2, 3, "a"]
同理,去除字符串里的重复字符
[...new Set('ababbc')].join('')
// abc
2.数组合并
- 常用的方法
.concat()
- es6 的“拉平”方法
.falt(num)
num不传则默认为1
let a = [1,2,3];
let b = [4,5,6];
[a,b].flat(); // [1,2,3,4,5,6]
数组嵌套数组的情况
var arr = [1, [[2, 3], 4], [5, 6]];
var flat = function* (a) {
a.forEach(function (item) {
if (typeof item !== 'number') {
yield* flat(item);
} else {
yield item;
}
});
};
for (var f of flat(arr)){
console.log(f);
}
数组的静态方法
Object.getOwnPropertyNames(Array);
// (6) ["length", "name", "prototype", "isArray", "from", "of"]
数组的实例方法
Object.getOwnPropertyNames(Array.prototype);
// (33) ["length", "constructor", "concat", "copyWithin", "fill", "find", "findIndex", "lastIndexOf", "pop", "push", "reverse", "shift", "unshift", "slice", "sort", "splice", "includes", "indexOf", "join", "keys", "entries", "values", "forEach", "filter", "flat", "flatMap", "map", "every", "some", "reduce", "reduceRight", "toLocaleString", "toString"]
网友评论