请各位大佬指点!js 数组对象去掉相同的谢谢哈~

原始数据:
(20)[{…},{…},{…},{…},{…},{…},__ob__:Observer]
0:
dataContent:(...)
topic:"年龄"
1:
dataContent:(...)
topic:"性别"
2:
dataContent:(...)
topic:"性别"
3:{__ob__:Observer}
4:{__ob__:Observer}
删除数组对象topic字段相同的,图中有两个topic:性别,只想留住后面的,要删除前面那个topic:性别的,把下标为1的删除留下标为2的期望得到:
(20)[{…},{…},{…},{…},{…},{…},__ob__:Observer]
0:
dataContent:(...)
topic:"年龄"
2:
dataContent:(...)
topic:"性别"
3:{__ob__:Observer}
4:{__ob__:Observer}
更新:topic字段值不固定
汪汪一只猫
浏览 281回答 2
2回答

一只斗牛犬

//传key值和value值数组function(keys,value){keys.forEach((k,i)=>{letindex=arr.findIndex(item=>item[k]==value[i])arr.splice(index,1)})}

扬帆大鱼

functiondemo(list){letkeys=[],item;for(leta=list.length-1;a>=0;a--){item=list[a]if(keys.some(t=>t===item.topic)){list.splice(a,1)}else{keys.push(item.topic)}}returnlist}vararr=[{name:'张三',topic:'班主任'},{name:'李四',topic:'体育老师'},{name:'王五',topic:'班主任'},{name:'赵六',topic:'体育老师'}]varres=demo(arr)console.log(JSON.stringify(res))//=>[{"name":"王五","topic":"班主任"},{"name":"赵六","topic":"体育老师"}]
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript