一、實現數組的去重:數組
1.一、方法一:oop
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.二、方法二:spa
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);
二、實現數組的過濾:code
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);
三、數組的相同屬性值的合併:對象
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"}
四、數組的扁平化:blog
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]
五、摘取數組對象的某幾個字段:ip
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}})
六、兩個數組拼接成對象數組:hash
方法一:it
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}]
方法二:io
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);