根据 javascript 中的 2 个键值对排序
Sorting based on 2 key value pairs in javascript
我有一个对象数组,每个对象都包含一个嵌套数组。我需要根据嵌套数组中的 ID 过滤主数组。应从结果中删除具有重复 ID 的对象。
对象数组具有嵌套数组,其中有 2 个参数:"AuN": "sanjay mittal"
和相应的 "AuId"
。我需要根据 "AuN": "sanjay mittal"
附带的 "AuId"
的值使我的主数组对象唯一
数据:
[
{
"CC": 623,
"AA": [
{
"AuN": "tayfun e tezduyar",
"AuId": 166714465
},
{
"AuN": "sanjay mittal",
"AuId": 2141608163
}
],
"PK": 2009478656
},
{
"CC": 488,
"AA": [
{
"AuN": "tayfun e tezduyar",
"AuId": 166714465
},
{
"AuN": "marek behr",
"AuId": 2117578800
},
{
"AuN": "sanjay mittal",
"AuId": 2141608163 // duplicate data. Need to remove the main object
}
],
"PK": 1973948837
},
{
"CC": 118,
"AA": [
{
"AuN": "rajendra k premchand",
"AuId": 1987062001
},
{
"AuN": "kamal sharma",
"AuId": 2426730634
},
{
"AuN": "sanjay mittal",
"AuId": 2583035735
},
{
"AuN": "inder s anand",
"AuId": 1907699963
}
],
"PK": 2116095873
},
{
"CC": 401,
"AA": [
{
"AuN": "sanjay mittal",
"AuId": 2123420744
},
{
"AuN": "brian falkenhainer",
"AuId": 68193438
}
],
"PK": 153230822
},
{
"CC": 86,
"AA": [
{
"AuN": "sanjay mittal",
"AuId": 2123420744
},
{
"AuN": "shulian shang",
"AuId": 2723036630
},
{
"AuN": "todd j mortier",
"AuId": 2063047595
},
{
"AuN": "cyril j schweich",
"AuId": 2132794328
}
],
"PK": 2021819698
}
]
预期结果:
我的预期结果删除了一些主要对象,基于名称 "AuN": "sanjay mittal"
和唯一性 "AuId"
:
var expectedArray = [
{
"CC": 623,
"AA": [
{
"AuN": "tayfun e tezduyar",
"AuId": 166714465
},
{
"AuN": "sanjay mittal",
"AuId": 2141608163
}
],
"PK": 2009478656
},
{
"CC": 118,
"AA": [
{
"AuN": "rajendra k premchand",
"AuId": 1987062001
},
{
"AuN": "kamal sharma",
"AuId": 2426730634
},
{
"AuN": "sanjay mittal",
"AuId": 2583035735
}
{
"AuN": "inder s anand",
"AuId": 1907699963
}
],
"PK": 2116095873
},
{
"CC": 401,
"AA": [
{
"AuN": "sanjay mittal",
"AuId": 2123420744
},
{
"AuN": "brian falkenhainer",
"AuId": 68193438
}
],
"PK": 153230822
},
]
我正试图找到一种有效的方法来做到这一点。任何见解和方法都可以帮助我实现这一目标。
您可以为此使用 Map
。通过相关的AuId对其进行键控,并为其存储对应的顶级对象。如果您以相反的顺序执行此操作,则第一次出现的相同 AuId 将保留在地图中。
然后只需从地图上再次取出这些对象即可:
const array = [{"CC":623,"AA":[{"AuN":"tayfun e tezduyar","AuId":166714465},{"AuN":"sanjay mittal","AuId":2141608163}],"PK":2009478656},{"CC":488,"AA":[{"AuN":"tayfun e tezduyar","AuId":166714465},{"AuN":"marek behr","AuId":2117578800},{"AuN":"sanjay mittal","AuId":2141608163}],"PK":1973948837},{"CC":118,"AA":[{"AuN":"rajendra k premchand","AuId":1987062001},{"AuN":"kamal sharma","AuId":2426730634},{"AuN":"sanjay mittal","AuId":2583035735},{"AuN":"inder s anand","AuId":1907699963}],"PK":2116095873},{"CC":401,"AA":[{"AuN":"sanjay mittal","AuId":2123420744},{"AuN":"brian falkenhainer","AuId":68193438}],"PK":153230822},{"CC":86,"AA":[{"AuN":"sanjay mittal","AuId":2123420744},{"AuN":"shulian shang","AuId":2723036630},{"AuN":"todd j mortier","AuId":2063047595},{"AuN":"cyril j schweich","AuId":2132794328}],"PK":2021819698}];
const map = new Map(array.reverse().map(obj =>
[obj.AA.find(({AuN}) => AuN === "sanjay mittal").AuId, obj]
));
const filtered = Array.from(map.values()).reverse();
console.log(filtered);
此代码假定每个对象都有这样一个 "sanjay mittal" 条目。如果不能保证这一点,那么您需要添加一些测试并决定如何处理这样的 "orphaned" 对象。
加法:
在您提到的评论中,您收到关于 AuId 属性 的错误。这意味着上面最后一段中的假设是不正确的。所以这里是你如何处理这种情况:
const array = [{"CC":623,"AA":[{"AuN":"tayfun e tezduyar","AuId":166714465},{"AuN":"sanjay mittal","AuId":2141608163}],"PK":2009478656},{"CC":488,"AA":[{"AuN":"tayfun e tezduyar","AuId":166714465},{"AuN":"marek behr","AuId":2117578800},{"AuN":"sanjay mittal","AuId":2141608163}],"PK":1973948837},{"CC":118,"AA":[{"AuN":"rajendra k premchand","AuId":1987062001},{"AuN":"kamal sharma","AuId":2426730634},{"AuN":"sanjay mittal","AuId":2583035735},{"AuN":"inder s anand","AuId":1907699963}],"PK":2116095873},{"CC":401,"AA":[{"AuN":"s*a*n*jay mittal","AuId":2123420744},{"AuN":"brian falkenhainer","AuId":68193438}],"PK":153230822},{"CC":86,"AA":[{"AuN":"sanjay mittal","AuId":2123420744},{"AuN":"shulian shang","AuId":2723036630},{"AuN":"todd j mortier","AuId":2063047595},{"AuN":"cyril j schweich","AuId":2132794328}],"PK":2021819698}];
const map = new Map(array.reverse().map((obj, i) => {
const found = obj.AA.find(({AuN}) => AuN === "sanjay mittal");
if (found) return [found, obj];
return [-i, [...obj.AA, { "AuN":"sanjay mittal","AuId": "missing" }]];
}));
const filtered = Array.from(map.values()).reverse();
console.log(filtered);
这将检测 "sanjay mittal" 条目的缺失,然后将认为该对象不是重复项,并将条目 "sanjay mittal"/"missing" 添加到过滤数据中。
我有一个对象数组,每个对象都包含一个嵌套数组。我需要根据嵌套数组中的 ID 过滤主数组。应从结果中删除具有重复 ID 的对象。
对象数组具有嵌套数组,其中有 2 个参数:"AuN": "sanjay mittal"
和相应的 "AuId"
。我需要根据 "AuN": "sanjay mittal"
"AuId"
的值使我的主数组对象唯一
数据:
[
{
"CC": 623,
"AA": [
{
"AuN": "tayfun e tezduyar",
"AuId": 166714465
},
{
"AuN": "sanjay mittal",
"AuId": 2141608163
}
],
"PK": 2009478656
},
{
"CC": 488,
"AA": [
{
"AuN": "tayfun e tezduyar",
"AuId": 166714465
},
{
"AuN": "marek behr",
"AuId": 2117578800
},
{
"AuN": "sanjay mittal",
"AuId": 2141608163 // duplicate data. Need to remove the main object
}
],
"PK": 1973948837
},
{
"CC": 118,
"AA": [
{
"AuN": "rajendra k premchand",
"AuId": 1987062001
},
{
"AuN": "kamal sharma",
"AuId": 2426730634
},
{
"AuN": "sanjay mittal",
"AuId": 2583035735
},
{
"AuN": "inder s anand",
"AuId": 1907699963
}
],
"PK": 2116095873
},
{
"CC": 401,
"AA": [
{
"AuN": "sanjay mittal",
"AuId": 2123420744
},
{
"AuN": "brian falkenhainer",
"AuId": 68193438
}
],
"PK": 153230822
},
{
"CC": 86,
"AA": [
{
"AuN": "sanjay mittal",
"AuId": 2123420744
},
{
"AuN": "shulian shang",
"AuId": 2723036630
},
{
"AuN": "todd j mortier",
"AuId": 2063047595
},
{
"AuN": "cyril j schweich",
"AuId": 2132794328
}
],
"PK": 2021819698
}
]
预期结果:
我的预期结果删除了一些主要对象,基于名称 "AuN": "sanjay mittal"
和唯一性 "AuId"
:
var expectedArray = [
{
"CC": 623,
"AA": [
{
"AuN": "tayfun e tezduyar",
"AuId": 166714465
},
{
"AuN": "sanjay mittal",
"AuId": 2141608163
}
],
"PK": 2009478656
},
{
"CC": 118,
"AA": [
{
"AuN": "rajendra k premchand",
"AuId": 1987062001
},
{
"AuN": "kamal sharma",
"AuId": 2426730634
},
{
"AuN": "sanjay mittal",
"AuId": 2583035735
}
{
"AuN": "inder s anand",
"AuId": 1907699963
}
],
"PK": 2116095873
},
{
"CC": 401,
"AA": [
{
"AuN": "sanjay mittal",
"AuId": 2123420744
},
{
"AuN": "brian falkenhainer",
"AuId": 68193438
}
],
"PK": 153230822
},
]
我正试图找到一种有效的方法来做到这一点。任何见解和方法都可以帮助我实现这一目标。
您可以为此使用 Map
。通过相关的AuId对其进行键控,并为其存储对应的顶级对象。如果您以相反的顺序执行此操作,则第一次出现的相同 AuId 将保留在地图中。
然后只需从地图上再次取出这些对象即可:
const array = [{"CC":623,"AA":[{"AuN":"tayfun e tezduyar","AuId":166714465},{"AuN":"sanjay mittal","AuId":2141608163}],"PK":2009478656},{"CC":488,"AA":[{"AuN":"tayfun e tezduyar","AuId":166714465},{"AuN":"marek behr","AuId":2117578800},{"AuN":"sanjay mittal","AuId":2141608163}],"PK":1973948837},{"CC":118,"AA":[{"AuN":"rajendra k premchand","AuId":1987062001},{"AuN":"kamal sharma","AuId":2426730634},{"AuN":"sanjay mittal","AuId":2583035735},{"AuN":"inder s anand","AuId":1907699963}],"PK":2116095873},{"CC":401,"AA":[{"AuN":"sanjay mittal","AuId":2123420744},{"AuN":"brian falkenhainer","AuId":68193438}],"PK":153230822},{"CC":86,"AA":[{"AuN":"sanjay mittal","AuId":2123420744},{"AuN":"shulian shang","AuId":2723036630},{"AuN":"todd j mortier","AuId":2063047595},{"AuN":"cyril j schweich","AuId":2132794328}],"PK":2021819698}];
const map = new Map(array.reverse().map(obj =>
[obj.AA.find(({AuN}) => AuN === "sanjay mittal").AuId, obj]
));
const filtered = Array.from(map.values()).reverse();
console.log(filtered);
此代码假定每个对象都有这样一个 "sanjay mittal" 条目。如果不能保证这一点,那么您需要添加一些测试并决定如何处理这样的 "orphaned" 对象。
加法:
在您提到的评论中,您收到关于 AuId 属性 的错误。这意味着上面最后一段中的假设是不正确的。所以这里是你如何处理这种情况:
const array = [{"CC":623,"AA":[{"AuN":"tayfun e tezduyar","AuId":166714465},{"AuN":"sanjay mittal","AuId":2141608163}],"PK":2009478656},{"CC":488,"AA":[{"AuN":"tayfun e tezduyar","AuId":166714465},{"AuN":"marek behr","AuId":2117578800},{"AuN":"sanjay mittal","AuId":2141608163}],"PK":1973948837},{"CC":118,"AA":[{"AuN":"rajendra k premchand","AuId":1987062001},{"AuN":"kamal sharma","AuId":2426730634},{"AuN":"sanjay mittal","AuId":2583035735},{"AuN":"inder s anand","AuId":1907699963}],"PK":2116095873},{"CC":401,"AA":[{"AuN":"s*a*n*jay mittal","AuId":2123420744},{"AuN":"brian falkenhainer","AuId":68193438}],"PK":153230822},{"CC":86,"AA":[{"AuN":"sanjay mittal","AuId":2123420744},{"AuN":"shulian shang","AuId":2723036630},{"AuN":"todd j mortier","AuId":2063047595},{"AuN":"cyril j schweich","AuId":2132794328}],"PK":2021819698}];
const map = new Map(array.reverse().map((obj, i) => {
const found = obj.AA.find(({AuN}) => AuN === "sanjay mittal");
if (found) return [found, obj];
return [-i, [...obj.AA, { "AuN":"sanjay mittal","AuId": "missing" }]];
}));
const filtered = Array.from(map.values()).reverse();
console.log(filtered);
这将检测 "sanjay mittal" 条目的缺失,然后将认为该对象不是重复项,并将条目 "sanjay mittal"/"missing" 添加到过滤数据中。