[英]How to create an array of object inside another array of object from an array of Objects in javascript
I want to create an array of objects inside I want to create one more array of objecrs from an array of objects.我想在里面创建一个对象数组 我想从一个对象数组中再创建一个对象数组。
I want array of object to be like this我希望 object 的数组是这样的
[
{
id: "1",
tasks: [
{
id: "12ef-3902",
title: "Title 1",
start: "2022-06-01"
end: "2022-09-02"
},
],
},
{
id: "2",
tasks: [
{
id: "12ef-3904",
title: "Title 2",
start: "2022-02-01",
end: "2022-04-02"
},
],
},
{
id: "3",
tasks: [
{
id: "12ef-3906",
title: "Title 3",
start: "2022-10-09",
end: "2022-12-02"
},
],
}
];
And this is my current array of objects这是我当前的对象数组
[
{
id:"1",
productId:"12ef-3902",
productName:"title 1",
startDate:"2022-06-01",
endDate:"2022-09-02"
},
{
id:"2",
productId:"12ef-3904",
productName:"title 2",
startDate:"2022-02-01",
endDate:"2022-04-02"
},
{
id:"3",
productId:"12ef-3906",
productName:"title 3",
startDate:"2022-10-09",
endDate:"2022-12-02"
}
]
Here is my code I am not sure how to get array of object inside tasks.这是我的代码,我不确定如何在任务中获取 object 数组。
var arr2 = listData.map((v) => ({
id: v.id,
}));
You can use map
to process your array, using object destructuring to simplify the code:您可以使用
map
来处理您的数组,使用 object 解构来简化代码:
listData = [{ id: "1", productId: "12ef-3902", productName: "title 1", startDate: "2022-06-01", endDate: "2022-09-02" }, { id: "2", productId: "12ef-3904", productName: "title 2", startDate: "2022-02-01", endDate: "2022-04-02" }, { id: "3", productId: "12ef-3906", productName: "title 3", startDate: "2022-10-09", endDate: "2022-12-02" } ]; result = listData.map(({ id, productId, productName, startDate, endDate}) => ({ id, tasks: [{ id: productId, title: productName, start: startDate, end: endDate }] }) ) console.log(result)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.