这是以前写的 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 const data = [{ id: 1 , title: "与想隆鼻客户打心理战" }, { id: 2 , title: "与想隆鼻客户打心理战" }, { id: 3 , title: "hhhhh" } ] let result = {}; let finalResult=[]; for (let i=0 ;i<data.length;i++){ result[data[i].title]=data[i]; } for (item in result){ finalResult.push (result[item]); } console .log ('原始数据:' , data, finalResult)
今天看了都不知道这是什么鬼,所以使用(ES6)优化下
这是现在优化 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 const data = [{ id: 1 , title: "与想隆鼻客户打心理战" }, { id: 2 , title: "与想隆鼻客户打心理战" }, { id: 3 , title: "hhhhh" } ] const finalResult = data.reduce ((acc, cur) => { !acc.map (i => i.title).includes (cur.title) && acc.push (cur) return acc },[]) console .log (finalResult)
Author: 蓝思同学
Permalink: https://www.lancema.com/posts/2020/JavaScript/JS通过键值(重复)过滤对象数组.html
Slogan: small is beautiful, small is powerful !