2 回答
TA贡献1998条经验 获得超6个赞
在这里,我使用 map 来遍历您的数组,在该迭代中,我再次使用 map 来遍历插槽。
let myCourses = [
{
course: "ee3001",
slots: [
{
day: "monday",
time: "0900-1100",
},
{
day: "tuesday",
time: "0930-1100",
},
{
day: "wednesday",
time: "1330-1530",
},
],
},
{
course: "ee3002",
slots: [
{
day: "monday",
time: "0900-1100",
},
{
day: "thursday",
time: "0930-1130",
},
],
},
{
course: "ee3003",
slots: [
{
day: "tuesday",
time: "0930-1100",
},
{
day: "wednesday",
time: "1330-1530",
},
{
day: "thursday",
time: "0930-1130",
},
],
},
];
const newArray=[]
myCourses.forEach(myFunction);
function myFunction(item, index) {
newArray[index] = [];
item.slots.map((child) =>
newArray[index].push({ course: item.course, slots: child })
);
}
TA贡献1801条经验 获得超8个赞
再见,你可以尝试使用这个例子:
let myCourses = [
{
course: "ee3001",
slots: [
{
day: "monday",
time: "0900-1100",
},
{
day: "tuesday",
time: "0930-1100",
},
{
day: "wednesday",
time: "1330-1530",
},
],
},
{
course: "ee3002",
slots: [
{
day: "monday",
time: "0900-1100",
},
{
day: "thursday",
time: "0930-1130",
},
],
},
{
course: "ee3003",
slots: [
{
day: "tuesday",
time: "0930-1100",
},
{
day: "wednesday",
time: "1330-1530",
},
{
day: "thursday",
time: "0930-1130",
},
],
},
];
let Arrayee3001 = [];
let array1 = myCourses.filter(course => course.course === "ee3001")
array1[0].slots.forEach(slot => {
let result = {};
result.course = array1[0].course;
result.slot = [];
result.slot.push(slot);
Arrayee3001.push(result);
});
console.log(Arrayee3001);
我已经myCourses根据courseid 过滤了数组。然后我为找到的每个插槽创建了一个 forEach slots,我创建了一个从数组中过滤得到的对象result,course以及当前的slot. 最后,我将这个result对象推送到名为Arrayee3001.
添加回答
举报