[英]Adding Property to another Array of objects from another set of Array of Objects
Lets say I have 2 Array of objects and I want to conditionally add one to the other.假设我有 2 个对象数组,我想有条件地将一个对象添加到另一个对象中。
let aData = [
{
id: 1,
item: "shirt",
price: "24.99",
quantity: "1"
},
{
id: 2,
item: "pants",
price: "34.99",
quantity: "5"
}
]
let bData = [
{
id: 1,
item_id: 1,
url: "imagelink.jpg"
},
{
id: 2,
item_id: 1,
url: "image2link.jpg"
},
{
id: 3,
item_id: 2,
url: "image3link.jpg"
},
{
id: 4,
item_id: 2,
url: "image4link.jpg"
{
]
What I would like is to add all of bData
to aData
if the bData[i].item_id == aData[i].id我想要的是将所有
bData
添加到aData
如果 bData[i].item_id == aData[i].id
let cData = [
{
id: 1,
item: "shirt",
price: "24.99",
quantity: "1",
images: [
{
id: 1,
item_id: 1,
url: "imagelink.jpg"
},
{
id: 2,
item_id: 1,
url: "image2link.jpg"
}
]
},
{
etc...
}
]
I am at a standstill and this is what I've came up with so far我处于停滞状态,这就是我到目前为止想出的
let temp = [];
for (i = 0; i < aData.length; i++) {
let temp2 = [];
temp2.push(aData[i]);
let tempImages = {};
for (k = 0; k < bData.length; k++) {
if (aData[i].id == bData[k].item_id) {
tempImages[k] = bData[k];
}
}
temp2.images = tempImages;
temp.push(temp2)
}
This is not doing what I need这不是我需要的
This code works for me.这段代码对我有用。
temp = []
for (i = 0; i < aData.length; i++) {
if (aData[i].id == bData[i].item_id) {
temp.push(bData[i]);
}
}
aData = aData.concat(temp);
console.log(aData);
I would rather to use map
and filter
to do the job in a very clean way.我宁愿使用
map
和filter
以非常干净的方式完成这项工作。
the code should be:代码应该是:
const result = aData.map(a => {
a.images = bData.filter(b => b.item_id === a.id);
return a;
})
here is a working example with your sample data.这是您的示例数据的工作示例。
let aData = [{ id: 1, item: "shirt", price: "24.99", quantity: "1" }, { id: 2, item: "pants", price: "34.99", quantity: "5" } ] let bData = [{ id: 1, item_id: 1, url: "imagelink.jpg" }, { id: 2, item_id: 1, url: "image2link.jpg" }, { id: 3, item_id: 2, url: "image3link.jpg" }, { id: 4, item_id: 2, url: "image4link.jpg" } ] const result = aData.map(a => { // use Object.assign to avoid changing the reference of aData const completeObject = Object.assign({}, a) completeObject.images = bData.filter(b => b.item_id === a.id); return completeObject; }) console.log(result)
You could collect all images first by a grouping with item_id
and map all items with a new property images
.您可以首先通过使用
item_id
和 map 所有具有新属性images
的项目进行分组来收集所有图像。
This approach need only one iteration for every array.这种方法只需要对每个数组进行一次迭代。
const items = [{ id: 1, item: "shirt", price: "24.99", quantity: "1" }, { id: 2, item: "pants", price: "34.99", quantity: "5" }], images = [{ id: 1, item_id: 1, url: "imagelink.jpg" }, { id: 2, item_id: 1, url: "image2link.jpg" }, { id: 3, item_id: 2, url: "image3link.jpg" }, { id: 4, item_id: 2, url: "image4link.jpg" }], imagesByItems = images.reduce((r, o) => ((r[o.item_id]??= []).push(o), r), {}), result = items.map(o => ({...o, images: imagesByItems[o.id] || [] })); console.log(result);
.as-console-wrapper { max-height: 100%;important: top; 0; }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.