
功能:对一个多维数组进行去重,条件是第一个参数“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://m.ipkd.cn/webs_14423.html
声明:本站提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请发送到邮箱:admin@ipkd.cn,我们会在看到邮件的第一时间内为您处理!

AI工具网址导航站
js如何把字符串转成json格式
如何靠蛙蛙写小说赚生活费,支持长短篇小说/剧本/小说拆书/推文视频
如何选择一家SEO优化比较靠谱的公司?