[英]Compare one array with a nested array and push value into a new array with same index in Javascript
I have 2 arrays我有 2 个数组
const arrayOne = [
{
id: '110'
},
{
id: '202'
},
{
id: '259'
}
];
const arrayTwo = [
{
data: [
{
value: 'Alpha',
id: '001'
}
],
{
data: [
{
value: 'Bravo',
id: '202'
}
]
}
];
I need to create a new array comparing arrayOne[idx].id
with arrayTwo[idx].data[idx2].id
我需要创建一个新数组,将arrayOne[idx].id
与arrayTwo[idx].data[idx2].id
进行比较
Upon match, I need to create an array pushing value ( arrayTwo[idx].data[idx2].value
) to the new array against each index in arrayOne
.匹配后,我需要针对arrayOne
中的每个索引创建一个数组推送值( arrayTwo[idx].data[idx2].value
)到新数组。
In this example, I would get newArr = [null, 'Bravo', null]
在这个例子中,我会得到newArr = [null, 'Bravo', null]
What I have tried:我试过的:
arrayOne.map(item => ({
...item,
result: arrayTwo.filter(itemTwo => item.data.map(x => x.id).includes(itemTwo.id))
}));
and also并且
const newArr = [];
arrayOne.map((item, idx) => {
if (arrayTwo.filter(itemTwo => itemTwo.data?.map(x => x.id) === item.id)) {
newArr.push(arrayTwo.data[idx].value);
} else newArr.push(null);
});
const arr1 = [ { id: '110' }, { id: '202' }, { id: '259' } ]; const arr2 = [ { data: [{ value: 'Alpha', id: '001' }] }, { data: [{ value: 'Bravo', id: '202' }] } ]; const result1 = arr1.map(o1 => { const data = arr2.map(o2 => o2.data).flat(); const value = data.filter(o2 => o2.id === o1.id)[0]?.value; return value ? {...o1, value} : o1; }); const result2 = arr1.map(o1 => { const data = arr2.map(o2 => o2.data).flat(); const value = data.filter(o2 => o2.id === o1.id)[0]?.value; return value ?? null; }); [result1, result2].forEach(r => console.log(JSON.stringify(r)));
This seems difficult at first because of how arrayTwo
is structured.起初这似乎很困难,因为arrayTwo
的结构。 We can make our lives much easier by converting it into a dictionary, then we can just map arrayOne
to get our results.我们可以通过将它转换成字典来使我们的生活更轻松,然后我们可以映射arrayOne
来获得我们的结果。
const arrayOne = [ {id: '110'}, {id: '202'}, {id: '259'} ]; const arrayTwo = [ {data: [{value: 'Alpha',id: '001'}]}, {data: [{value: 'Bravo',id: '202'}]} ]; const dict = {}; arrayTwo.forEach((obj) => { let key = obj.data[0].id let value = obj.data[0].value dict[key] = value } ) const result = arrayOne.map(obj => (dict[obj.id] || null)) console.log('Output: ', result) console.log('Dictionary:', dict)
Array.prototype.reduce()
:
这也可以通过Array.prototype.reduce()
来完成:
const arrayOne = [ {id: '110'}, {id: '202'}, {id: '259'} ]; const arrayTwo = [ {data: [{value: 'Alpha',id: '001'}]}, {data: [{value: 'Bravo',id: '202'}]} ]; const dict = arrayTwo.reduce((output, obj) => { let key = obj.data[0].id let value = obj.data[0].value output[key] = value return output }, {}) // dict = {'001':'Alpha', '202': 'Bravo'} const result = arrayOne.map(obj => (dict[obj.id] || null)) console.log(result)
You can try this easy line of code :你可以试试这行简单的代码:
const arrayOne = [{ id: '110' }, { id: '202' }, { id: '259' }]; const arrayTwo = [{ data: [{ value: 'Alpha', id: '001' }], }, { data: [{ value: 'Bravo', id: '202' }] }]; let result = arrayOne.map(el => { let found = arrayTwo.find(f => f.data.at(0)?.id == el.id)?.data.at(0)?.value; return { id: el.id, value: found ?? null}; }); console.log(result);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.