gpt4 book ai didi

javascript - 将包含空数组的对象数组映射为属性会忽略整个对象

转载 作者:行者123 更新时间:2023-12-01 16:09:43 25 4
gpt4 key购买 nike

我有一个对象数组 -

let initialArr = [
{
"id": 1,
"name": "product name",
"product_details": [
{
"id": 1,
"details": "some details"
}
],
"subscriptions": [
{
"id": 1,
"subs": "7 days"
},
{
"id": 2,
"subs": "15 days"
}
]
},
{
"id": 2,
"name": "product name 2",
"product_details": [
{
"id": 2,
"details": "some details 2"
}
],
"subscriptions": [
{
"id": 1,
"subs": "7 days"
},
{
"id": 2,
"subs": "15 days"
}
]
},
{
"id": 3,
"name": "product name 3",
"product_details": [
{
"id": 3,
"details": "some details 3"
}
],
"subscriptions": []
}
]

这就是我想要实现的——

[
{
"id": 1,
"name": "product name",
"detailsId" : 1,
"details": "some details"
"subsId": 1,
"subs": "7 days"
},
{
"id": 1,
"name": "product name",
"detailsId" : 1,
"details": "some details"
"subsId": 2,
"subs": "15 days"
},
{
"id": 2,
"name": "product name 2",
"detailsId" : 2,
"details": "some details 2"
"subsId": 1,
"subs": "7 days"
},
{
"id": 2,
"name": "product name 2",
"detailsId" : 2,
"details": "some details 2"
"subsId": 2,
"subs": "15 days"
},
{
"id": 3,
"name": "product name 3",
"detailsId" : 3,
"details": "some details 3"
}
]

这就是我所做的-

initialArr.map(e => {
e.product_details.map(p =>{
e.subscriptions.map(s => {
newArr.push({
id: e.id,
name: e.name,
detailsId: p.id,
details: p.details,
subsId: s.id,
subs:s.subs
});
});
})
})

如果 subscriptions 数组不为空,则此方法有效。如果对于某些产品,订阅数组为空,则该产品不会被推送到数组中。在我无法弄清楚如何解决它。

第三个产品没有被插入新数组。这就是我得到的 -

[
{
"id": 1,
"name": "product name",
"detailsId" : 1,
"details": "some details"
"subsId": 1,
"subs": "7 days"
},
{
"id": 1,
"name": "product name",
"detailsId" : 1,
"details": "some details"
"subsId": 2,
"subs": "15 days"
},
{
"id": 2,
"name": "product name 2",
"detailsId" : 2,
"details": "some details 2"
"subsId": 1,
"subs": "7 days"
},
{
"id": 2,
"name": "product name 2",
"detailsId" : 2,
"details": "some details 2"
"subsId": 2,
"subs": "15 days"
}
]

注意:虽然同一个产品在新数组中重复了两次,但这是要求 - 产品根据订阅数组的“subs”属性。

假设我有更多数组,例如除了“订阅”之外的“自定义”、“订单”等,我还希望推送这些数组的数据,这是对多个数组执行的正确方法吗?

最佳答案

假设数组 product_details 始终包含 1 个元素及其数据,这是一个解决方案。
使用 Array#reduce累积新的结果数组。为每个元素创建一个新的临时对象,其中包含所有元素的数据。如果订阅数组为空,则将此临时对象推送到您累积的结果数组。否则使用 Array#forEach迭代您的订阅。对于每个订阅使用 Object.assign复制您的临时对象。添加订阅数据并将其推送到结果数组。

const initialArr = [{ id: 1, name: "product name", product_details: [{ id: 1, details: "some details" }], subscriptions: [{ id: 1, subs: "7 days" }, { id: 2, subs: "15 days" }] }, { id: 2, name: "product name 2", product_details: [{ id: 2, details: "some details 2" }], subscriptions: [{ id: 1, subs: "7 days" }, { id: 2, subs: "15 days" }] }, { id: 3, name: "product name 3", product_details: [{ id: 3, details: "some details 3" }], subscriptions: [] }];

let res = initialArr.reduce((acc, cur) => {
let temp = {
id: cur.id,
name: cur.name,
detailsId: cur.product_details[0].id,
details: cur.product_details[0].details
}
if (!cur.subscriptions.length)
acc.push(temp);
else {
cur.subscriptions.forEach(subs => {
let tempSub = Object.assign({}, temp);
tempSub.subsId = subs.id;
tempSub.subs = subs.subs;
acc.push(tempSub);
})
}
return acc;
}, []);

console.log(res);

这里是一个没有 reduce 而是 forEach 的版本:

const initialArr = [{ id: 1, name: "product name", product_details: [{ id: 1, details: "some details" }], subscriptions: [{ id: 1, subs: "7 days" }, { id: 2, subs: "15 days" }] }, { id: 2, name: "product name 2", product_details: [{ id: 2, details: "some details 2" }], subscriptions: [{ id: 1, subs: "7 days" }, { id: 2, subs: "15 days" }] }, { id: 3, name: "product name 3", product_details: [{ id: 3, details: "some details 3" }], subscriptions: [] }];

let acc = [];
initialArr.forEach(cur => {
let temp = {
id: cur.id,
name: cur.name,
detailsId: cur.product_details[0].id,
details: cur.product_details[0].details
}
if (!cur.subscriptions.length)
acc.push(temp);
else {
cur.subscriptions.forEach(subs => {
let tempSub = Object.assign({}, temp);
tempSub.subsId = subs.id;
tempSub.subs = subs.subs;
acc.push(tempSub);
})
}
});

console.log(acc);

关于javascript - 将包含空数组的对象数组映射为属性会忽略整个对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63658278/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com