Javascript 基于属性值删除重复项
我想根据属性值从响应中删除重复数组。如果Javascript 基于属性值删除重复项,javascript,filter,Javascript,Filter,我想根据属性值从响应中删除重复数组。如果属性值数据与其他数组属性值匹配,则应删除其他属性值 逻辑很简单。检查每个数组中的重复属性_值,删除重复数组并返回 作为回应现在您可以看到属性值=1是三倍 属性值=2是两倍 如果看到属性值重复,如何比较和删除整个数组 我尝试了过滤方法,但似乎不起作用。请帮忙 for(var j=0; j<social_post_link.length; j++){ newFilterarray = social_post_link[j].activity_at
属性值
数据与其他数组属性值匹配,则应删除其他属性值
逻辑很简单。检查每个数组中的重复属性_值,删除重复数组并返回
作为回应现在您可以看到属性值=1是三倍
属性值=2是两倍
如果看到属性值重复,如何比较和删除整个数组
我尝试了过滤方法,但似乎不起作用。请帮忙
for(var j=0; j<social_post_link.length; j++){
newFilterarray = social_post_link[j].activity_attributes[0].attribute_value.filter(function(item, index) {
if (social_post_link[j].activity_attributes[0].attribute_value.indexOf(item) == index){
return social_post_link;
}
});
}
期望输出
[
{
"id": "484822",
"activity_attributes": [
{
"id": "868117",
"activity_id": "484822",
"attribute_name": "position",
"attribute_value": "1",
}
]
},
{
"id": "484823",
"activity_attributes": [
{
"id": "868120",
"activity_id": "484823",
"attribute_name": "position",
"attribute_value": "2",
}
]
},
{
"id": "484891",
"activity_attributes": [
{
"id": "868189",
"attribute_name": "position",
"attribute_value": "3",
}
]
},
{
"id": "484903",
"activity_attributes": [
{
"id": "868200",
"attribute_name": "position",
"attribute_value": "4",
},
]
}
]
一个简单的解决方案是使用lodash的
sortedUniqBy
功能:
const值=[{
“id”:“484822”,
“活动属性”:[{
“id”:“868117”,
“活动id”:“484822”,
“属性名称”:“位置”,
“属性值”:“1”,
}]
},
{
“id”:“484884”,
“活动属性”:[{
“id”:“868175”,
“活动id”:“484884”,
“属性名称”:“位置”,
“属性值”:“1”,
}]
},
{
“id”:“484888”,
“活动属性”:[{
“id”:“868182”,
“活动id”:“484888”,
“属性名称”:“位置”,
“属性值”:“1”,
}]
},
{
“id”:“484823”,
“活动属性”:[{
“id”:“868120”,
“活动id”:“484823”,
“属性名称”:“位置”,
“属性值”:“2”,
}]
},
{
“id”:“484975”,
“活动属性”:[{
“id”:“868344”,
“属性名称”:“位置”,
“属性值”:“2”,
}]
},
{
“id”:“484891”,
“活动属性”:[{
“id”:“868189”,
“属性名称”:“位置”,
“属性值”:“3”,
}]
},
{
“id”:“484903”,
“活动属性”:[{
“id”:“868200”,
“属性名称”:“位置”,
“属性值”:“4”,
}, ]
}
];
const result=uu.sortedUniqBy(值、函数(项){
返回项。活动属性[0]。属性值;
});
控制台日志(结果)代码>
请发布输入数据,而不是图像,以及所需的输出。使用filter()
函数时,只需根据过滤条件返回布尔值即可。在您的情况下,在if
条件中返回true
。最后返回false
。@Ele删除了图像,并用初始数组的响应结果进行了更新。请发布所需的输出,以便更好地了解所需内容。我想检查第一个数组“活动属性”的“属性值”。如果值重复,则删除重复值。
[
{
"id": "484822",
"activity_attributes": [
{
"id": "868117",
"activity_id": "484822",
"attribute_name": "position",
"attribute_value": "1",
}
]
},
{
"id": "484823",
"activity_attributes": [
{
"id": "868120",
"activity_id": "484823",
"attribute_name": "position",
"attribute_value": "2",
}
]
},
{
"id": "484891",
"activity_attributes": [
{
"id": "868189",
"attribute_name": "position",
"attribute_value": "3",
}
]
},
{
"id": "484903",
"activity_attributes": [
{
"id": "868200",
"attribute_name": "position",
"attribute_value": "4",
},
]
}
]