gpt4 book ai didi

javascript - 合并每个键有多个匹配项的两个 Javascript 对象

转载 作者:行者123 更新时间:2023-12-03 02:55:43 25 4
gpt4 key购买 nike

我有两个对象正在尝试合并。我几乎成功地回答了这个问题( Merge two unordered objects with different keys but same value? )。

问题是在我的“transpatterns”对象中,我有两个与“Intercom”键匹配的模式。然而,在我的输出中,只需要第二个。我需要它在同一个键下列出两者。

这是我的代码:

var hash = {};

var partitions = [{
'$': {
uuid: '{0F314A21-B066-B597-BCFA-6EC88CF8813B}'
},
name: ['AllPhones']
},
{
'$': {
uuid: '{4DCF3A89-ADA1-2770-4154-F6E0204D9A71}'
},
name: ['Unity_Connection']
},
{
'$': {
uuid: '{B653BC00-2D90-790C-AE0D-16C3DD2F8EDD}'
},
name: ['LAB-Tansform-Calling']
},
{
'$': {
uuid: '{148C8971-87E4-49D3-1536-69B6C95293B3}'
},
name: ['Blocked']
},
{
'$': {
uuid: '{25534E0F-F69A-FDDB-2FAD-34312BB6CEFC}'
},
name: ['LAB-PSTN-Sim-911']
},
{
'$': {
uuid: '{EC1E144B-2C8C-22CC-F1F5-423CF0343367}'
},
name: ['LAB-PSTN-Sim-Local']
},
{
'$': {
uuid: '{9C7F3223-AE2D-7605-C642-0CEAB81CD555}'
},
name: ['LAB-PSTN-Sim-LD']
},
{
'$': {
uuid: '{15AFADC3-859B-2806-B9D1-5678CE035E3D}'
},
name: ['LAB-PSTN-Sim-Intl']
},
{
'$': {
uuid: '{5DD43CF0-5FB0-A6A0-A71D-D284265F37C7}'
},
name: ['Agents']
},
{
'$': {
uuid: '{7FDE7E11-F821-2491-BB08-E6A41DAB205D}'
},
name: ['UCCX']
},
{
'$': {
uuid: '{17500618-9567-92B9-8C15-95D794094A3F}'
},
name: ['Intercom']
},
{
'$': {
uuid: '{77049844-AB06-F899-26D0-C0940E20D4D5}'
},
name: ['Netech_Managers']
},
{
'$': {
uuid: '{A9DD47E0-1C4A-AAB9-E859-E6EEDB26E6B0}'
},
name: ['Assistant_Route_Point']
}
];

var transpatterns = [{
'$': {
uuid: '{3FF82ECB-7364-41DB-176B-65D28885339F}'
},
pattern: ['9.19005554444'],
description: ['Sample Outbound Call Block'],
routePartitionName: ['Blocked']
},
{
'$': {
uuid: '{18C2C829-AFAE-DB28-A003-5F1731907D85}'
},
pattern: ['555555XXXX'],
description: ['Inbound DID Range'],
routePartitionName: ['AllPhones']
},
{
'$': {
uuid: '{465EFEF8-C405-8D30-2C97-4BF9907C92C6}'
},
pattern: ['3100'],
description: ['Intercom Xlate'],
routePartitionName: ['Intercom']
},
{
'$': {
uuid: '{C17E117C-41C7-D459-8CD1-B1D3C4EDC40D}'
},
pattern: ['3101'],
description: ['Intercom Xlate'],
routePartitionName: ['Intercom']
}
];


// TESTING NEW
// COMBINE OBJECTS - CREATE HASH
function classify(e) {
if (hash[e.name] || hash[e.routePartitionName]) {
Object.keys(e).forEach(function (c) {
hash[e.name || e.routePartitionName][c] = e[c];
});
} else {
hash[e.routePartitionName || e.name] = e;
}
}

// COMBINE OBJECTS - ADD VARS to HASH
partitions.forEach(classify);
transpatterns.forEach(classify);

// COMBINE OBJECTS - ITERATE HASH + REMOVE ANY FIELDS
var combo = Object.keys(hash).map(function (e) {
delete hash[e]['routePartitionName'];
return hash[e];
});

console.log(combo);

最佳答案

我能够解决我的问题。我确定在合并两个对象之前需要折叠具有与同一键关联的多个项目的对象。我能够使用这个问题的答案来帮助我做到这一点。 How to combine JSON object with same key and add their other corresponding values?

这是我想出的代码,看起来效果很好。

// DEFINE VARIABLES
var partitions = [{
'$': {
uuid: '{0F314A21-B066-B597-BCFA-6EC88CF8813B}'
},
name: ['AllPhones']
},
{
'$': {
uuid: '{4DCF3A89-ADA1-2770-4154-F6E0204D9A71}'
},
name: ['Unity_Connection']
},
{
'$': {
uuid: '{B653BC00-2D90-790C-AE0D-16C3DD2F8EDD}'
},
name: ['LAB-Tansform-Calling']
},
{
'$': {
uuid: '{148C8971-87E4-49D3-1536-69B6C95293B3}'
},
name: ['Blocked']
},
{
'$': {
uuid: '{25534E0F-F69A-FDDB-2FAD-34312BB6CEFC}'
},
name: ['LAB-PSTN-Sim-911']
},
{
'$': {
uuid: '{EC1E144B-2C8C-22CC-F1F5-423CF0343367}'
},
name: ['LAB-PSTN-Sim-Local']
},
{
'$': {
uuid: '{9C7F3223-AE2D-7605-C642-0CEAB81CD555}'
},
name: ['LAB-PSTN-Sim-LD']
},
{
'$': {
uuid: '{15AFADC3-859B-2806-B9D1-5678CE035E3D}'
},
name: ['LAB-PSTN-Sim-Intl']
},
{
'$': {
uuid: '{5DD43CF0-5FB0-A6A0-A71D-D284265F37C7}'
},
name: ['Agents']
},
{
'$': {
uuid: '{7FDE7E11-F821-2491-BB08-E6A41DAB205D}'
},
name: ['UCCX']
},
{
'$': {
uuid: '{17500618-9567-92B9-8C15-95D794094A3F}'
},
name: ['Intercom']
},
{
'$': {
uuid: '{77049844-AB06-F899-26D0-C0940E20D4D5}'
},
name: ['Netech_Managers']
},
{
'$': {
uuid: '{A9DD47E0-1C4A-AAB9-E859-E6EEDB26E6B0}'
},
name: ['Assistant_Route_Point']
}
];

var transpatterns = [{
'$': {
uuid: '{3FF82ECB-7364-41DB-176B-65D28885339F}'
},
pattern: ['9.19005554444'],
description: ['Sample Outbound Call Block'],
routePartitionName: ['Blocked']
},
{
'$': {
uuid: '{18C2C829-AFAE-DB28-A003-5F1731907D85}'
},
pattern: ['555555XXXX'],
description: ['Inbound DID Range'],
routePartitionName: ['AllPhones']
},
{
'$': {
uuid: '{465EFEF8-C405-8D30-2C97-4BF9907C92C6}'
},
pattern: ['3100'],
description: ['Intercom Xlate'],
routePartitionName: ['Intercom']
},
{
'$': {
uuid: '{C17E117C-41C7-D459-8CD1-B1D3C4EDC40D}'
},
pattern: ['3101'],
description: ['Intercom Xlate'],
routePartitionName: ['Intercom']
}
];

var hash = {};

// FUNCTION - COMBINE ITEMS WITHIN OBJECT - WITH SAME KEY:routePartitionName
function combine(arr) {
var combined = arr.reduce(function (result, item) {
var current = result[item.routePartitionName];

result[item.routePartitionName] = !current ? item : {
pattern: current.pattern + ',' + item.pattern,
description: item.description,
routePartitionName: item.routePartitionName
};

return result;
}, {});

return Object.keys(combined).map(function (key) {
return combined[key];
});
}

// FUNCTION - COMBINE ITEMS WITHIN OBJECT - RUN
var result = combine(transpatterns);

// FUNCTION - COMBINE OBJECTS - CREATE HASH
function classify(e) {
if (hash[e.name] || hash[e.routePartitionName]) {
Object.keys(e).forEach(function (c) {
hash[e.name || e.routePartitionName][c] = e[c];
});
} else {
hash[e.routePartitionName || e.name] = e;
}
}

// FUNCTION - COMBINE OBJECTS - ADD VARS to HASH
partitions.forEach(classify);
result.forEach(classify);

// FUNCTION - COMBINE OBJECTS - ITERATE HASH + REMOVE ANY FIELDS
var combo = Object.keys(hash).map(function (e) {
delete hash[e]['routePartitionName'];
delete hash[e]['$'];
return hash[e];
});

console.log(combo);

关于javascript - 合并每个键有多个匹配项的两个 Javascript 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47638322/

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