a数组:
[{ id: 1, key: 'key1', value: 'ggg' }, { id: 2, key: 'key2', value: 'cccc' }]
b数组
{ id: 4, key1: '123', key2: 'sdfdff' }
期望
[
{ id: 1, key: 'key1', value: 'ggg', key1: '123' },
{ id: 2, key: 'key2', value: 'cccc', key2: 'sdfdff' }
]
a数组:
[{ id: 1, key: 'key1', value: 'ggg' }, { id: 2, key: 'key2', value: 'cccc' }]
b数组
{ id: 4, key1: '123', key2: 'sdfdff' }
期望
[
{ id: 1, key: 'key1', value: 'ggg', key1: '123' },
{ id: 2, key: 'key2', value: 'cccc', key2: 'sdfdff' }
]
应该使用 hash table 提升效率
const arrA = [{ id: 1, key: 'key1', value: 'ggg' }, { id: 2, key: 'key2', value: 'cccc' }]
const arrB = { id: 4, key1: '123', key2: 'sdfdff' }
const bHash = Object.fromEntries(
Object.entries(arrB).filter(
([key]) => key.startsWith(key)
).map(([key, value]) => [key, value])
)
arrA.forEach(item => {
item[item.key] = bHash[item.key]
})
10 回答11.2k 阅读
5 回答4.9k 阅读✓ 已解决
4 回答3.1k 阅读✓ 已解决
2 回答2.8k 阅读✓ 已解决
3 回答4.9k 阅读✓ 已解决
1 回答3.3k 阅读✓ 已解决
3 回答2.4k 阅读✓ 已解决