删除重复项但不将副本保存在数组中
Removing duplicates but not saving the copy in array
我能够从下面给出的 JSON 中删除重复项:
var testJSON=[
{
"target": "300.0",
"valueObj": {
"id": 2538
}
},
{
"target": "400.0",
"valueObj": {
"id": 2539
}
},
{
"target": "300.0",
"valueObj": {
"id": 2538
}
},
{
"target": "400.0",
"valueObj": {
"id": 2539
}
},
{
"target": "12.23",
"valueObj": {
"id": 2540
}
}
]
使用下划线 _uniq 属性 作为:
_.uniq(testJSON ,'valueObj.id');
其中 returns 唯一 属性 即,
{
"target": "300.0",
"valueObj": {
"id": 2538
}
}
但不显示其他对象。
我期待的结果..
[
{
"target": "300.0",
"valueObj": {
"id": 2538
}
},
{
"target": "400.0",
"valueObj": {
"id": 2539
}
},
{
"target": "12.23",
"valueObj": {
"id": 2540
}
}
]
通过这样做解决了..
var testJSON = _.uniq(testJSON, function (item: any, key, a) {
return item.valueObj.id;
});
我能够从下面给出的 JSON 中删除重复项:
var testJSON=[
{
"target": "300.0",
"valueObj": {
"id": 2538
}
},
{
"target": "400.0",
"valueObj": {
"id": 2539
}
},
{
"target": "300.0",
"valueObj": {
"id": 2538
}
},
{
"target": "400.0",
"valueObj": {
"id": 2539
}
},
{
"target": "12.23",
"valueObj": {
"id": 2540
}
}
]
使用下划线 _uniq 属性 作为:
_.uniq(testJSON ,'valueObj.id');
其中 returns 唯一 属性 即,
{
"target": "300.0",
"valueObj": {
"id": 2538
}
}
但不显示其他对象。 我期待的结果..
[
{
"target": "300.0",
"valueObj": {
"id": 2538
}
},
{
"target": "400.0",
"valueObj": {
"id": 2539
}
},
{
"target": "12.23",
"valueObj": {
"id": 2540
}
}
]
通过这样做解决了..
var testJSON = _.uniq(testJSON, function (item: any, key, a) {
return item.valueObj.id;
});