gpt4 book ai didi

javascript - 如何使用 AngularJS 应用过滤器来获取 JSON 数组内的 JSONobjects 中具有相同值的元素

转载 作者:行者123 更新时间:2023-12-03 05:54:27 28 4
gpt4 key购买 nike

我有一个包含多个(动态)JSON 对象的 JSON 数组。我需要比较对象并选择所有对象中具有相同值的键。我的 JSON 看起来像,

[
{
"CreateAccountName":"Joseph",
"CreateDateTime":"0001-01-01T00:00:00",
"Description":"Utilization of services",
"Type":2,
"Id":1000000001,
"Count":1,
"ModifiedAccountName":"",
"ModifiedDateTime":"2016-10-04T10:16:40.5190025",
"Name":"Utilization of services",
"CardCount":0,
"Target":95,
"UniversalId":"SDFOPIJ-SDFGLKJ-ER234-234LF",
"AccountId":0,
"AccountName":"Joseph"
},

{
"CreateAccountName":"Joseph",
"CreateDateTime":"0001-01-01T00:00:00",
"Description":"Utilization of Food",
"Type":2,
"Id":1000000001,
"Count":1,
"ModifiedAccountName":"",
"ModifiedDateTime":"2016-10-04T10:16:40.5190025",
"Name":"Utilization of Food",
"CardCount":0,
"Target":95,
"UniversalId":"SDFOPIJ-SDFGLKJ-ER234-234LF",
"AccountId":0,
"AccountName":"Joseph"
},

{
"CreateAccountName":"Joseph",
"CreateDateTime":"0001-01-01T00:00:00",
"Description":"Utilization of services",
"Type":2,
"Id":1000000001,
"Count":1,
"ModifiedAccountName":"",
"ModifiedDateTime":"2016-10-04T10:16:40.5190025",
"Name":"Utilization of services",
"CardCount":0,
"Target":95,
"UniversalId":"SDFOPIJ-SDFGLKJ-ER234-234LF",
"AccountId":0,
"AccountName":"Joseph"
}
]

我想获得具有相同值的 key 。例如,创建帐户名称。它在所有对象中具有相同的值。

棘手的情况是, key 本身是动态的。我无法对 key 进行硬编码并进行比较。键名称可能会更改,或者可能会出现另一组键。我正在寻找一种通用的解决方案来比较和获取对象的“交集”。

最佳答案

您只需使用Array.reduce()即可做到这一点

var json = [
{
"CreateAccountName":"Joseph",
"CreateDateTime":"0001-01-01T00:00:00",
"Description":"Utilization of services",
"Type":2,
"Id":1000000001,
"Count":1,
"ModifiedAccountName":"",
"ModifiedDateTime":"2016-10-04T10:16:40.5190025",
"Name":"Utilization of services",
"CardCount":0,
"Target":95,
"UniversalId":"SDFOPIJ-SDFGLKJ-ER234-234LF",
"AccountId":0,
"AccountName":"Joseph"
},

{
"CreateAccountName":"Joseph",
"CreateDateTime":"0001-01-01T00:00:00",
"Description":"Utilization of Food",
"Type":2,
"Id":1000000001,
"Count":1,
"ModifiedAccountName":"",
"ModifiedDateTime":"2016-10-04T10:16:40.5190025",
"Name":"Utilization of Food",
"CardCount":0,
"Target":95,
"UniversalId":"SDFOPIJ-SDFGLKJ-ER234-234LF",
"AccountId":0,
"AccountName":"Joseph"
},

{
"CreateAccountName":"Joseph",
"CreateDateTime":"0001-01-01T00:00:00",
"Description":"Utilization of services",
"Type":2,
"Id":1000000001,
"Count":1,
"ModifiedAccountName":"",
"ModifiedDateTime":"2016-10-04T10:16:40.5190025",
"Name":"Utilization of services",
"CardCount":0,
"Target":95,
"UniversalId":"SDFOPIJ-SDFGLKJ-ER234-234LF",
"AccountId":0,
"AccountName":"Joseph"
}
];

var intersection = json.reduce(function(result, item) {
Object.keys(result).forEach(function(key) {
if(! item.hasOwnProperty(key) || item[key] !== result[key]) delete result[key];
});
return result;
});

console.log(intersection)

关于javascript - 如何使用 AngularJS 应用过滤器来获取 JSON 数组内的 JSONobjects 中具有相同值的元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39998740/

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