Javascript: 合并对象数组
Javascript: Merging Object Array
我有一个包含嵌套对象的对象数组,如下所示:
[
{
"metric1": 4.1,
"addons": {
"call_uuid_1": "13d1e097-0363-4a81-b9e4-85c6a770cdb2",
"ThisDN_1": "50002",
"call_uuid": "13d1e097-0363-4a81-b9e4-85c6a770cdb4",
"ThisDN": "50004"
}
},
{
"metric2": -12345.123412341234,
"addons": {
"call_uuid_2": "13d1e097-0363-4a81-b9e4-85c6a770cdb3",
"ThisDN_2": "50003",
"call_uuid": "13d1e097-0363-4a81-b9e4-85c6a770cdb4",
"ThisDN": "50004"
}
},
{
"metric3": -2345.123412341234,
"addons": {
"call_uuid_1": "13d1e097-0363-4a81-b9e4-85c6a770cdb2",
"ThisDN_1": "50002",
"call_uuid": "13d1e097-0363-4a81-b9e4-85c6a770cdb4",
"ThisDN": "50004"
}
},
{
"metric4": -345.12341234123414,
"addons": {
"call_uuid_2": "13d1e097-0363-4a81-b9e4-85c6a770cdb3",
"ThisDN_2": "50003",
"call_uuid": "13d1e097-0363-4a81-b9e4-85c6a770cdb4",
"ThisDN": "50004"
}
}
]
我想合并一些数组对象与同一个嵌套对象addons
,这样之前的数组就变成:
[
{
"metric1": 4.1,
"metric3": -2345.123412341234,
"addons": {
"call_uuid_1
": "13d1e097-0363-4a81-b9e4-85c6a770cdb2",
"ThisDN_1": "50002",
"call_uuid": "13d1e097-0363-4a81-b9e4-85c6a770cdb4",
"ThisDN": "50004"
}
},
{
"metric2": -12345.123412341234,
"metric4": -345.12341234123414,
"addons": {
"call_uuid_2": "13d1e097-0363-4a81-b9e4-85c6a770cdb3",
"ThisDN_2": "50003",
"call_uuid": "13d1e097-0363-4a81-b9e4-85c6a770cdb4",
"ThisDN": "50004"
}
}
]
有没有办法做到这一点?
您可以使用 JSON.stringify
在 addons
对象中寻找相等性,然后合并您的对象。像这样:
const array = [
{
"metric1": 4.1,
"addons": {
"call_uuid_1": "13d1e097-0363-4a81-b9e4-85c6a770cdb2",
"ThisDN_1": "50002",
"call_uuid": "13d1e097-0363-4a81-b9e4-85c6a770cdb4",
"ThisDN": "50004"
}
},
{
"metric2": -12345.123412341234,
"addons": {
"call_uuid_2": "13d1e097-0363-4a81-b9e4-85c6a770cdb3",
"ThisDN_2": "50003",
"call_uuid": "13d1e097-0363-4a81-b9e4-85c6a770cdb4",
"ThisDN": "50004"
}
},
{
"metric3": -2345.123412341234,
"addons": {
"call_uuid_1": "13d1e097-0363-4a81-b9e4-85c6a770cdb2",
"ThisDN_1": "50002",
"call_uuid": "13d1e097-0363-4a81-b9e4-85c6a770cdb4",
"ThisDN": "50004"
}
},
{
"metric4": -345.12341234123414,
"addons": {
"call_uuid_2": "13d1e097-0363-4a81-b9e4-85c6a770cdb3",
"ThisDN_2": "50003",
"call_uuid": "13d1e097-0363-4a81-b9e4-85c6a770cdb4",
"ThisDN": "50004"
}
}
];
const mergedArray = [];
const addonsToIndexMap = {};
array.forEach(item => {
const stringifyAddon = JSON.stringify(item.addons);
let indexInTheMap = addonsToIndexMap[stringifyAddon];
if(indexInTheMap !== undefined) {
mergedArray[indexInTheMap] = {...mergedArray[indexInTheMap], ...item};
} else {
mergedArray.push(item);
addonsToIndexMap[stringifyAddon] = mergedArray.length - 1;
}
});
console.log(mergedArray);
使用addonsToIndexMap
只是为了优化目的。
注意: 此方法仅在 addon
对象中的键顺序相同时有效。
此外,我纠正了我程序中的输入,以更新密钥 call_uuid_l
和 call_uuid_1
以获得预期的输出,如评论中所述。请相应修改。
如果数组中有多个元素具有相同的 addons
对象,您可以删除第一个的 addons
对象,然后将其与其他元素合并。
for (let i = 0; i < arr.length; i++) {
for (let j = i + 1; j < arr.length; j++) {
if (JSON.stringify(arr[i]["addons"]) === JSON.stringify(arr[j]["addons"])) {
delete arr[i]["addons"];
Object.assign(arr[i], arr[j]);
arr.splice(j, 1);
j -= 1;
}
}
}
输出:
[
{
metric1: 4.1,
metric3: -2345.123412341234,
addons: {
call_uuid_1: '13d1e097-0363-4a81-b9e4-85c6a770cdb2',
ThisDN_1: '50002',
call_uuid: '13d1e097-0363-4a81-b9e4-85c6a770cdb4',
ThisDN: '50004'
}
},
{
metric2: -12345.123412341234,
metric4: -345.12341234123414,
addons: {
call_uuid_2: '13d1e097-0363-4a81-b9e4-85c6a770cdb3',
ThisDN_2: '50003',
call_uuid: '13d1e097-0363-4a81-b9e4-85c6a770cdb4',
ThisDN: '50004'
}
}
]
我有一个包含嵌套对象的对象数组,如下所示:
[
{
"metric1": 4.1,
"addons": {
"call_uuid_1": "13d1e097-0363-4a81-b9e4-85c6a770cdb2",
"ThisDN_1": "50002",
"call_uuid": "13d1e097-0363-4a81-b9e4-85c6a770cdb4",
"ThisDN": "50004"
}
},
{
"metric2": -12345.123412341234,
"addons": {
"call_uuid_2": "13d1e097-0363-4a81-b9e4-85c6a770cdb3",
"ThisDN_2": "50003",
"call_uuid": "13d1e097-0363-4a81-b9e4-85c6a770cdb4",
"ThisDN": "50004"
}
},
{
"metric3": -2345.123412341234,
"addons": {
"call_uuid_1": "13d1e097-0363-4a81-b9e4-85c6a770cdb2",
"ThisDN_1": "50002",
"call_uuid": "13d1e097-0363-4a81-b9e4-85c6a770cdb4",
"ThisDN": "50004"
}
},
{
"metric4": -345.12341234123414,
"addons": {
"call_uuid_2": "13d1e097-0363-4a81-b9e4-85c6a770cdb3",
"ThisDN_2": "50003",
"call_uuid": "13d1e097-0363-4a81-b9e4-85c6a770cdb4",
"ThisDN": "50004"
}
}
]
我想合并一些数组对象与同一个嵌套对象addons
,这样之前的数组就变成:
[
{
"metric1": 4.1,
"metric3": -2345.123412341234,
"addons": {
"call_uuid_1
": "13d1e097-0363-4a81-b9e4-85c6a770cdb2",
"ThisDN_1": "50002",
"call_uuid": "13d1e097-0363-4a81-b9e4-85c6a770cdb4",
"ThisDN": "50004"
}
},
{
"metric2": -12345.123412341234,
"metric4": -345.12341234123414,
"addons": {
"call_uuid_2": "13d1e097-0363-4a81-b9e4-85c6a770cdb3",
"ThisDN_2": "50003",
"call_uuid": "13d1e097-0363-4a81-b9e4-85c6a770cdb4",
"ThisDN": "50004"
}
}
]
有没有办法做到这一点?
您可以使用 JSON.stringify
在 addons
对象中寻找相等性,然后合并您的对象。像这样:
const array = [
{
"metric1": 4.1,
"addons": {
"call_uuid_1": "13d1e097-0363-4a81-b9e4-85c6a770cdb2",
"ThisDN_1": "50002",
"call_uuid": "13d1e097-0363-4a81-b9e4-85c6a770cdb4",
"ThisDN": "50004"
}
},
{
"metric2": -12345.123412341234,
"addons": {
"call_uuid_2": "13d1e097-0363-4a81-b9e4-85c6a770cdb3",
"ThisDN_2": "50003",
"call_uuid": "13d1e097-0363-4a81-b9e4-85c6a770cdb4",
"ThisDN": "50004"
}
},
{
"metric3": -2345.123412341234,
"addons": {
"call_uuid_1": "13d1e097-0363-4a81-b9e4-85c6a770cdb2",
"ThisDN_1": "50002",
"call_uuid": "13d1e097-0363-4a81-b9e4-85c6a770cdb4",
"ThisDN": "50004"
}
},
{
"metric4": -345.12341234123414,
"addons": {
"call_uuid_2": "13d1e097-0363-4a81-b9e4-85c6a770cdb3",
"ThisDN_2": "50003",
"call_uuid": "13d1e097-0363-4a81-b9e4-85c6a770cdb4",
"ThisDN": "50004"
}
}
];
const mergedArray = [];
const addonsToIndexMap = {};
array.forEach(item => {
const stringifyAddon = JSON.stringify(item.addons);
let indexInTheMap = addonsToIndexMap[stringifyAddon];
if(indexInTheMap !== undefined) {
mergedArray[indexInTheMap] = {...mergedArray[indexInTheMap], ...item};
} else {
mergedArray.push(item);
addonsToIndexMap[stringifyAddon] = mergedArray.length - 1;
}
});
console.log(mergedArray);
使用addonsToIndexMap
只是为了优化目的。
注意: 此方法仅在 addon
对象中的键顺序相同时有效。
此外,我纠正了我程序中的输入,以更新密钥 call_uuid_l
和 call_uuid_1
以获得预期的输出,如评论中所述。请相应修改。
如果数组中有多个元素具有相同的 addons
对象,您可以删除第一个的 addons
对象,然后将其与其他元素合并。
for (let i = 0; i < arr.length; i++) {
for (let j = i + 1; j < arr.length; j++) {
if (JSON.stringify(arr[i]["addons"]) === JSON.stringify(arr[j]["addons"])) {
delete arr[i]["addons"];
Object.assign(arr[i], arr[j]);
arr.splice(j, 1);
j -= 1;
}
}
}
输出:
[
{
metric1: 4.1,
metric3: -2345.123412341234,
addons: {
call_uuid_1: '13d1e097-0363-4a81-b9e4-85c6a770cdb2',
ThisDN_1: '50002',
call_uuid: '13d1e097-0363-4a81-b9e4-85c6a770cdb4',
ThisDN: '50004'
}
},
{
metric2: -12345.123412341234,
metric4: -345.12341234123414,
addons: {
call_uuid_2: '13d1e097-0363-4a81-b9e4-85c6a770cdb3',
ThisDN_2: '50003',
call_uuid: '13d1e097-0363-4a81-b9e4-85c6a770cdb4',
ThisDN: '50004'
}
}
]