es6 实现数组的操作

时间:2020-12-02 02:01:38

1、实现数组的去重:

  1.1、方法一:

let arr = [{id: 1, name: 'aa'},  {id: 2, name: 'bb'}, {id: 3, name: 'cc'}, {id: 4, name: 'dd'}, {id: 5, name: 'ee'}, {id: 1, name: 'aa'}, {id: 1, name: 'aa'}, {id: 1, name: 'aa'}, {id: 1, name: 'aa'}];

function unique(array) {
const res = new Map();
return array.filter((arr) => !res.has(arr.id) && res.set(arr.id, 1)); //用列表的id判断是否有重复
} unique(arr);

  1.2、方法二:

let hash = {},
arr = [{id: 1, name: 'aa'}, {id: 2, name: 'bb'}, {id: 3, name: 'cc'}, {id: 4, name: 'dd'}, {id: 5, name: 'ee'}, {id: 1, name: 'aa'}, {id: 1, name: 'aa'}, {id: 1, name: 'aa'}, {id: 1, name: 'aa'}]; const newArr = arr.reduceRight((item, next) => {
hash[next.id] ? '' : hash[next.id] = true && item.push(next);
return item;
}, []); console.log(newArr);

2、实现数组的过滤:

let arr = [{id: 1, name: 'aa'},  {id: 2, name: 'bb'}, {id: 3, name: 'cc'}, {id: 4, name: 'dd'}, {id: 5, name: 'ee'}];

function filter(arrayList, id) {
let temp = arrayList.filter((list) => {
if(list.id === id) { //过滤列表id等于id值,相反也可以用"!=="来过滤不等于id值的列表
return list;
}
});
return temp;
} filter(arr, 1);  

3、数组的相同属性值的合并:

let a = [{id: 1, name: 'a'}, {id: 2, name: 'b'}, {id: 1, name: 'c'},{id: 3, name: 'd'}, {id: 2, name: 'e'}, {id: 4, name: 'f'}];

let b = a.reduce((memo, {id, name}) => {
!memo[id] ? memo[id] = name : memo[id] += name;
return memo;
}, {});  //{1: "ac", 2: "be", 3: "d", 4: "f"}

4、数组的扁平化:

function convert(val) {
return val.reduce((init, next) => {
if(typeof next === 'object') {
next.forEach(item => {
if(typeof item === 'object') {
init = init.concat(convert(item));
} else {
init.push(item);
}
});
} else {
init.push(next);
}
return init;
}, [])
} Array.from(new Set(convert([1, 2, [3, 2, 1, [1, 2, 3, 4]]]))); //[1, 2, 3, 4]

5、摘取数组对象的某几个字段:

var arr = [
{
'id': '1',
'name': 'img1',
'imgUrl': './img1.jpg',
},
{
'id': '2',
'name': 'img2',
'imgUrl': './img2.jpg',
},
{
'id': '3',
'name': 'img3',
'imgUrl': './img3.jpg',
}
];
arr.map(x => {return {'imgUrl': x.imgUrl, 'name': x.name}})

6、两个数组拼接成对象数组:

  方法一:

let metrodates = ['2008-01', '2008-02', '2008-03'];
let figures = [0, 0.555, 0.293];
let output = metrodates.map((date, i) =>({date, data: figures [i]}));
console.log(output); //[{date: "2008-01", data: 0}, {date: "2008-02", data: 0.555}, {date: "2008-03", data: 0.293}]

  方法二:

const zip = ([x, ...xs],  [y, ...ys]) => {
if(x === undefined || y === undefined)
return [];
else
return [[x, y], ...zip(xs, ys)];
}
let metrodates = ['2008-01', '2008-02', '2008-03'];
let figures = [0, 0.555, 0.293];
let output = zip(metrodates, figures).map(([date, data]) =>({date, data}));
console.log(output); //[{date: "2008-01", data: 0}, {date: "2008-02", data: 0.555}, {date: "2008-03", data: 0.293}]

  方法三:

const isEmpty = xs => xs.length === 0;
const head =([x, ...xs]) => x;
const tail =([x, ...xs]) => xs;
const map = (f, ...xxs) => {
let loop = (acc, xxs) => {
if(xxs.some(isEmpty))
return acc;
else
return loop([...acc, f(...xxs.map(head))], xxs.map(tail));
};
return loop([], xxs);
};
let metrodates = ['2008-01', '2008-02', '2008-03'];
let figures = [0, 0.555, 0.293];
let output = map((date, data) =>({date, data}), metrodates, figures);
console.log(output);