здесь у меня есть два массива объектов, которые выглядят так,
const arr1 = [
{
_id: "63e5cbadd926a20ade863c44",
productId: "63de474a561e0319a574552b"
},
{
_id: "63e5cbadd926a20ade863c45",
productId: "63de47c7561e0319a5745531"
},
{
_id: "63e5cbadd926a20ade863c46",
productId: "63dea93bdf662740f4ba37fe"
}
]
и другой массив выглядит так,
const arr2 = [
{
_id: "63de474a561e0319a574552b",
categoryOneId: [Object],
productPrice: 439.89
},
{
_id: "63de47c7561e0319a5745531",
categoryOneId: [Object],
productPrice: 56.9
},
{
_id: "63dea93bdf662740f4ba37fe",
categoryOneId: [Object],
productPrice: 56.9
}
]
теперь мне нужно что-то вроде ниже, где мне нужны все элементы из второго массива и нужно поле productId
, прикрепленное к каждому объекту. Пожалуйста, найдите ожидаемый результат ниже.
[
{
_id: "63de474a561e0319a574552b",
categoryOneId: [Object],
productPrice: 439.89,
orderId: "63e5cbadd926a20ade863c44"
},
{
_id: "63de47c7561e0319a5745531",
categoryOneId: [Object],
productPrice: 56.9,
orderId: "63e5cbadd926a20ade863c45"
},
{
_id: "63dea93bdf662740f4ba37fe",
categoryOneId: [Object],
productPrice: 56.9,
orderId: "63e5cbadd926a20ade863c46"
}
]
Я пытался что-то вроде этого,
for (let i = 0; i < arr2.length; i++) {
const element = arr2[i];
if (productIds.find(e => e.productId === element._id )) {
arr2[i].productId = arr1[i].productId
}
}
может кто-нибудь, пожалуйста, помогите мне исправить это.
Спасибо.
const arr3 = arr2.map((item, key) => ({...item, orderId: arr1[key]._id}))
если массивы перемешиваются:
const arr3 = arr2.map((item, key) => {
const foundItem = arr1.find(e => e.productId === item._id);
return ({
...item,
orderId: foundItem ? foundItem._id: null
});
})
Воспользуйтесь картой и найдите
const arr1 = [{
_id: "63e5cbadd926a20ade863c44",
productId: "63de474a561e0319a574552b"
},
{
_id: "63e5cbadd926a20ade863c45",
productId: "63de47c7561e0319a5745531"
},
{
_id: "63e5cbadd926a20ade863c46",
productId: "63dea93bdf662740f4ba37fe"
}
]
const arr2 = [{
_id: "63de474a561e0319a574552b",
categoryOneId: "[Object]",
productPrice: 439.89
},
{
_id: "63de47c7561e0319a5745531",
categoryOneId: "[Object]",
productPrice: 56.9
},
{
_id: "63dea93bdf662740f4ba37fe",
categoryOneId: "[Object]",
productPrice: 56.9
}
]
const arr = arr2.map(item => ({
...item,
orderId: arr1.find(order => order.productId === item._id)._id,
}))
console.info(arr);
const arr1 = [{
_id: "63e5cbadd926a20ade863c44",
productId: "63de474a561e0319a574552b"
},
{
_id: "63e5cbadd926a20ade863c45",
productId: "63de47c7561e0319a5745531"
},
{
_id: "63e5cbadd926a20ade863c46",
productId: "63dea93bdf662740f4ba37fe"
}
]
const arr2 = [{
_id: "63de474a561e0319a574552b",
categoryOneId: "[Object]",
productPrice: 439.89
},
{
_id: "63de47c7561e0319a5745531",
categoryOneId: "[Object]",
productPrice: 56.9
},
{
_id: "63dea93bdf662740f4ba37fe",
categoryOneId: "[Object]",
productPrice: 56.9
}
]
// This attempts to correct your approach
// You did not update the element correctly.
// Iterative Approach
const output = [];
for (let i = 0; i < arr2.length; i++) {
const element = arr2[i];
if (item = arr1.find(e => e.productId === element._id )) {
output.push({ ...element, orderId: item._id})
}
}
// Idiomatic Javascript
const output1 = arr2.map(element => {
if (item = arr1.find(({productId}) => element._id === productId)) {
return {...element, orderId: item._id}
}
})
// Functional Style.. eh for learning purposes only. it's an overkill here
const output2 = arr2.reduce((acc, element) => {
if (item = arr1.find(e => e.productId === element._id )) {
return acc.concat({...element, orderId: item._id})
}
}, [])
console.info("Iterative Approach", output)
console.info("Idiomatic Approach", output1)
console.info("Functional Approach", output2)
ваш arr2 и ожидаемый arr совпадают, пожалуйста, предоставьте более подробную информацию