功能:对一个多维数组进行去重,条件是第一个参数“name”相同,第二个参数“age”相同,输出数据!
具体实现代码:
var data = [ { name: '小米', age: '23', value: 1 }, { name: '小明', age: '31', value: 2 }, { name: '小米', age: '22', value: 3 }, { name: '小花', age: '25', value: 3 }, { name: '小明', age: '31', value: 4 }, { name: '小米', age: '23', value: 4 }, ]; var uniqueByKeys = data.reduce((aList, cItem) => { var key = `${cItem.name}-${cItem.age}`; var foundIndex = aList.findIndex(item => `${item.name}-${item.age}` === key); if (foundIndex === -1) { aList.push(cItem); } else { // 如何想处理重复项,可以在这里操作 // 例如:aList[foundIndex].value = Math.max(aList[foundIndex].value, cItem.value); } return aList; }, []); console.log("输出结果:",uniqueByKeys);
输出结果:
输出结果: (4) [{…}, {…}, {…}, {…}] 0: {name: '小米', age: '23', value: 1} 1: {name: '小明', age: '31', value: 2} 2: {name: '小米', age: '22', value: 3} 3: {name: '小花', age: '25', value: 3} length: 4
上面是“js多维数组去重:条件是“key1”相同并且“key2”相同进行去重”的全面内容,想了解更多关于 js 内容,请继续关注web建站教程。
当前网址:https://ipkd.cn/webs_14423.html
workflows工作流
一座巨大的野兽派建筑漂浮在热带风景中的贫民
一辆老式灵车在黑暗中从雾中出现ComfyUI工作流
一支令人难忘的狙击步枪ComfyUI工作流
一匹热血沸腾的汗血宝马庄严地站立着ComfyUI工作流
严寒的冬天里一只小鸟在森林的树枝上休息
树上挂着一只快乐的小樱桃
一块慕斯蛋糕ComfyUI工作流
一群蚂蚁在路上忙碌着ComfyUI工作流
猜你喜欢
声明:本站提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请发送到邮箱:admin@ipkd.cn,我们会在看到邮件的第一时间内为您处理!