gpt4 book ai didi

javascript - 如何使用js查找对象数组中的重复数据?

转载 作者:行者123 更新时间:2023-12-02 23:03:50 25 4
gpt4 key购买 nike

我有以下对象数组,例如

[  
{
"id":"30772",
"posimage":"/b/l/blue-shirt_1_1.jpg",
"position":"Position Front",
"tech":"Screen Print"
},
{
"id":"30771",
"posimage":"/b/l/blue-shirt_3.jpg",
"position":"Position Front",
"tech":"Screen Print"
},
{
"id":"30772",
"posimage":"/b/l/blue-shirt_1_1.jpg",
"position":"Position Front",
"tech":"Embroidery"
},
{
"id":"30771",
"posimage":"/b/l/blue-shirt_3.jpg",
"position":"Position Front",
"tech":"Embroidery"
}
]

这里我有重复的值,例如“id”:“30772”,“position”:“Position Front”我需要获取重复的位置,并且需要在警告框中给出,例如位置前面重复

最佳答案

const items = [{  
"id":"30772",
"posimage":"/b/l/blue-shirt_1_1.jpg",
"position":"Position Front",
"tech":"Screen Print"
},
{
"id":"30771",
"posimage":"/b/l/blue-shirt_3.jpg",
"position":"Position Front",
"tech":"Screen Print"
},
{
"id":"30772",
"posimage":"/b/l/blue-shirt_1_1.jpg",
"position":"Position Front",
"tech":"Embroidery"
},
{
"id":"30771",
"posimage":"/b/l/blue-shirt_3.jpg",
"position":"Position Front",
"tech":"Embroidery"
}]

items.forEach(item => {
const result = items.filter(it => it.id === item.id)
if (result.length > 1) {
return 'duplicate value exists';
}
return 'no duplicates';
})

关于javascript - 如何使用js查找对象数组中的重复数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57680014/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com