原始数据:(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字段值不固定
2 回答
一只斗牛犬
TA贡献1784条经验 获得超2个赞
//传key值和value值数组function(keys,value){keys.forEach((k,i)=>{letindex=arr.findIndex(item=>item[k]==value[i])arr.splice(index,1)})}
扬帆大鱼
TA贡献1799条经验 获得超9个赞
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":"体育老师"}]
添加回答
举报
0/150
提交
取消