根据对象内的两个值来合并两个数组



在使用映射和条件时,我面临一个问题,基本上如示例所示,我有两个对象数组,我需要根据对象的两个属性来区分:location_id和location_id。

const addedItems = [
{
"item_variation_id": "itemvar_QQWn8cQxq7TnB6",
"quantity": 5,
"location_id": "loc_r91C120vrwlReF",
"current_stock": 0,
"action": "add_stock",
},
{
"item_variation_id": "itemvar_QQWn8cQxq7TnB6",
"quantity": 8,
"location_id": "loc_5MHY8nXyvSIqh2",
"current_stock": -2,
"action": "add_stock",
},
{
"item_variation_id": "itemvar_QQWn8cQxq7TnB6",
"quantity": 15,
"location_id": "loc_5BpxGVWA9g09vf",
"current_stock": 0,
"action": "add_stock",
}
];
const preItems = [
{
"item_variation_id": "itemvar_O49yq8yCDC2v1N",
"quantity": null,
"location_id": "loc_r91C120vrwlReF",
"current_stock": 59,
"action": "",
},
{
"item_variation_id": "itemvar_O49yq8yCDC2v1N",
"quantity": null,
"location_id": "loc_5MHY8nXyvSIqh2",
"current_stock": 8,
"action": "",
},
{
"item_variation_id": "itemvar_O49yq8yCDC2v1N",
"quantity": null,
"location_id": "loc_5BpxGVWA9g09vf",
"current_stock": 1,
"action": "",
},
{
"item_variation_id": "itemvar_duW8Yhhd4aT1iF",
"quantity": null,
"location_id": "loc_r91C120vrwlReF",
"current_stock": 27,
"action": "",
},
{
"item_variation_id": "itemvar_duW8Yhhd4aT1iF",
"quantity": null,
"location_id": "loc_5MHY8nXyvSIqh2",
"current_stock": 0,
"action": "",
},
{
"item_variation_id": "itemvar_duW8Yhhd4aT1iF",
"quantity": null,
"location_id": "loc_5BpxGVWA9g09vf",
"current_stock": -4,
"action": "",
},
{
"item_variation_id": "itemvar_QQWn8cQxq7TnB6",
"quantity": 5,
"location_id": "loc_r91C120vrwlReF",
"current_stock": 0,
"action": "add_stock",
},
{
"item_variation_id": "itemvar_QQWn8cQxq7TnB6",
"quantity": null,
"location_id": "loc_5MHY8nXyvSIqh2",
"current_stock": -2,
"action": "",
},
{
"item_variation_id": "itemvar_QQWn8cQxq7TnB6",
"quantity": null,
"location_id": "loc_5BpxGVWA9g09vf",
"current_stock": 0,
"action": "",
},
{
"item_variation_id": "itemvar_YASfOt2B6B4ixE",
"quantity": null,
"location_id": "loc_r91C120vrwlReF",
"current_stock": 0,
"action": "",
}
]
const findItem = (id) => addedItems.find( itemData => itemData.item_variation_id === id )
const updatedItems = [...preItems.map( item => 
( item.item_variation_id === findItem(item.item_variation_id)?.item_variation_id && 
item.location_id === findItem(item.item_variation_id)?.location_id 
) ? findItem(item.item_variation_id) : item
)  ]
console.log(updatedItems)

总之,我想要得到的是

first: compare the two arrays (addeedItems and preItems)
second: match these keys while iterating: item_variation_id and location_id
third: if those two match values(item_variation_id and location_id) matches while iterating, remove the item from preItem and add the item from addedItems

综上所述,我的函数updatedItems只删除一个项目,而预期的输出应该移动3个项目。因此输出将是3个值将被更新为各自的数量

提前感谢您的帮助

您可以只映射preItems数组,并在每次迭代中测试adddeditems数组是否匹配。如果存在,则返回addeditems匹配,否则返回preItems匹配。有几种方法可以做到这一点。我用的是array.prototype.find

let updated = preItems.map(p => {
let b = addedItems.find(a => a.location_id === p.location_id && a.item_variation_id === p.item_variation_id);
return b ? b : p;
});

const addedItems = [{
"item_variation_id": "itemvar_QQWn8cQxq7TnB6",
"quantity": 5,
"location_id": "loc_r91C120vrwlReF",
"current_stock": 0,
"action": "add_stock",
"xtra": " ADDED1!!!"
},
{
"item_variation_id": "itemvar_QQWn8cQxq7TnB6",
"quantity": 8,
"location_id": "loc_5MHY8nXyvSIqh2",
"current_stock": -2,
"action": "add_stock",
"xtra": " ADDED2!!!"
},
{
"item_variation_id": "itemvar_QQWn8cQxq7TnB6",
"quantity": 15,
"location_id": "loc_5BpxGVWA9g09vf",
"current_stock": 0,
"action": "add_stock",
"xtra": " ADDED3!!!"
}
];
const preItems = [{
"item_variation_id": "itemvar_O49yq8yCDC2v1N",
"quantity": null,
"location_id": "loc_r91C120vrwlReF",
"current_stock": 59,
"action": "",
},
{
"item_variation_id": "itemvar_O49yq8yCDC2v1N",
"quantity": null,
"location_id": "loc_5MHY8nXyvSIqh2",
"current_stock": 8,
"action": "",
},
{
"item_variation_id": "itemvar_O49yq8yCDC2v1N",
"quantity": null,
"location_id": "loc_5BpxGVWA9g09vf",
"current_stock": 1,
"action": "",
},
{
"item_variation_id": "itemvar_duW8Yhhd4aT1iF",
"quantity": null,
"location_id": "loc_r91C120vrwlReF",
"current_stock": 27,
"action": "",
},
{
"item_variation_id": "itemvar_duW8Yhhd4aT1iF",
"quantity": null,
"location_id": "loc_5MHY8nXyvSIqh2",
"current_stock": 0,
"action": "",
},
{
"item_variation_id": "itemvar_duW8Yhhd4aT1iF",
"quantity": null,
"location_id": "loc_5BpxGVWA9g09vf",
"current_stock": -4,
"action": "",
},
{
"item_variation_id": "itemvar_QQWn8cQxq7TnB6",
"quantity": 5,
"location_id": "loc_r91C120vrwlReF",
"current_stock": 0,
"action": "add_stock",
},
{
"item_variation_id": "itemvar_QQWn8cQxq7TnB6",
"quantity": null,
"location_id": "loc_5MHY8nXyvSIqh2",
"current_stock": -2,
"action": "",
},
{
"item_variation_id": "itemvar_QQWn8cQxq7TnB6",
"quantity": null,
"location_id": "loc_5BpxGVWA9g09vf",
"current_stock": 0,
"action": "",
},
{
"item_variation_id": "itemvar_YASfOt2B6B4ixE",
"quantity": null,
"location_id": "loc_r91C120vrwlReF",
"current_stock": 0,
"action": "",
}
]
let updated = preItems.map(p => {
let b = addedItems.find(a => a.location_id === p.location_id && a.item_variation_id === p.item_variation_id);
return b ? b : p;
});
console.log(updated)

相关内容

  • 没有找到相关文章

最新更新