如何合并此对象数组
[
{
substoreUuid: '1450215d-e1a4-454d-8042-014d0f70f01e',
storeUuid: '6627a93e-1e16-4e08-9057-44ce701ba169'
}
]
到这个对象数组,其中键名是相同的
[
{
acceptanceTaskDetailsUuid: '080d7831-9cab-4bf4-af92-bae7b75bd50d',
storeUuid: '6627a93e-1e16-4e08-9057-44ce701ba169',
quantity: 2,
uuid: '799898cc-f74a-42d9-9fa2-84da238d6b39'
},
{
acceptanceTaskDetailsUuid: '080d7831-9cab-4bf4-af92-bae7b75bd50d',
storeUuid: 'a0e64311-2410-48be-b97d-17c68a0ad2a1',
quantity: 1,
uuid: '211c626c-e8f7-40d7-b42a-690e2fa0082b'
}
]
如您所见,我需要合并来自两个不同数组的对象。
预期产出:
[
{
substoreUuid: '1450215d-e1a4-454d-8042-014d0f70f01e',
quantity: 2,
uuid: '799898cc-f74a-42d9-9fa2-84da238d6b39',
storeUuid: '6627a93e-1e16-4e08-9057-44ce701ba169',
}
]
这是我到目前为止所拥有的
const substoreArr = [
{
substoreUuid: '1450215d-e1a4-454d-8042-014d0f70f01e',
storeUuid: '6627a93e-1e16-4e08-9057-44ce701ba169'
}
];
const distributionStore = [
{
acceptanceTaskDetailsUuid: '080d7831-9cab-4bf4-af92-bae7b75bd50d',
storeUuid: '6627a93e-1e16-4e08-9057-44ce701ba169',
quantity: 2,
uuid: '799898cc-f74a-42d9-9fa2-84da238d6b39'
},
{
acceptanceTaskDetailsUuid: '080d7831-9cab-4bf4-af92-bae7b75bd50d',
storeUuid: 'a0e64311-2410-48be-b97d-17c68a0ad2a1',
quantity: 1,
uuid: '211c626c-e8f7-40d7-b42a-690e2fa0082b'
}
];
const newArr = distributionStore.map(diststore => {
for (const sub of substoreArr) {
if (sub.store_uuid === diststore.storeUuid) {
return diststore;
}
}
}); //output [undefined, undefined]
输出将删除不相关的storeUuid
并删除一些密钥。如何以更快的方式将其合并到vanilla js中。
答案 0 :(得分:0)
这是我到目前为止所学到的。我使用g++ -dM -E foo.cpp > defines.h
映射到相同的密钥
reduce
欢迎任何改进
答案 1 :(得分:0)
我已经为您的问题找到了可行的解决方案。您可能应该重构此代码并将操作拆分为单独的函数。
const result = substoreArr.reduce((acc, val) => {
const filtered = distributionStore
.filter(e => e.storeUuid === val.storeUuid)
.map(e => ({...e, ...val}));
if (filtered.length > 0) {
return [...acc, ...filtered];
} else {
return acc;
}
}, []);
答案 2 :(得分:0)
首先按storeUuid
过滤结果,然后映射结果,只获取必需的属性。所有代码都是vanilla Javascript ES6。
const substoreArr = [
{
substoreUuid: '1450215d-e1a4-454d-8042-014d0f70f01e',
storeUuid: '6627a93e-1e16-4e08-9057-44ce701ba169'
}
];
const distributionStore = [
{
acceptanceTaskDetailsUuid: '080d7831-9cab-4bf4-af92-bae7b75bd50d',
storeUuid: '6627a93e-1e16-4e08-9057-44ce701ba169',
quantity: 2,
uuid: '799898cc-f74a-42d9-9fa2-84da238d6b39'
},
{
acceptanceTaskDetailsUuid: '080d7831-9cab-4bf4-af92-bae7b75bd50d',
storeUuid: 'a0e64311-2410-48be-b97d-17c68a0ad2a1',
quantity: 1,
uuid: '211c626c-e8f7-40d7-b42a-690e2fa0082b'
}
];
const newArr = distributionStore
.filter(x => substoreArr.find(i => i.storeUuid === x.storeUuid))
.map(({ storeUuid, quantity, uuid }) => ({ storeUuid, quantity, uuid, ...substoreArr.find(i => i.storeUuid === storeUuid) }));
console.log(newArr);
答案 3 :(得分:0)
通过使用storeUuid作为键来构建子目录查找,可以实现有效的解决方案,以避免嵌套循环。
具有嵌套循环意味着您必须比较第一个数组中的每个项目,以及第二个数组中的每个项目,这将花费与n1 * n2
成比例的时间。但是如果你首先构建了substore的hashmap,你最终会做两个独立的循环,所以所花费的时间与n1 + n1
成正比,对于更大的数组来说,这将更快。
const substoreArr = [
{
substoreUuid: '1450215d-e1a4-454d-8042-014d0f70f01e',
storeUuid: '6627a93e-1e16-4e08-9057-44ce701ba169'
}
];
const distributionStore = [
{
acceptanceTaskDetailsUuid: '080d7831-9cab-4bf4-af92-bae7b75bd50d',
storeUuid: '6627a93e-1e16-4e08-9057-44ce701ba169',
quantity: 2,
uuid: '799898cc-f74a-42d9-9fa2-84da238d6b39'
},
{
acceptanceTaskDetailsUuid: '080d7831-9cab-4bf4-af92-bae7b75bd50d',
storeUuid: 'a0e64311-2410-48be-b97d-17c68a0ad2a1',
quantity: 1,
uuid: '211c626c-e8f7-40d7-b42a-690e2fa0082b'
}
];
// build an object (or hash) for substores, where the key is the storeUuid for fast lookup
const substoresByStoreUuid = {}
substoreArr.forEach(substore =>
substoresByStoreUuid[substore.storeUuid]=substore
)
const result = distributionStore
// remove items that don't have a matching substore
.filter(store => substoresByStoreUuid[store.storeUuid])
// return the merged properties
.map(({quantity, uuid, storeUuid}) => ({
quantity,
uuid,
storeUuid,
substoreUuid: substoresByStoreUuid[storeUuid].substoreUuid
}))