2 回答

TA貢獻(xiàn)1998條經(jīng)驗(yàn) 獲得超6個(gè)贊
在這里,我使用 map 來遍歷您的數(shù)組,在該迭代中,我再次使用 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貢獻(xiàn)1801條經(jīng)驗(yàn) 獲得超8個(gè)贊
再見,你可以嘗試使用這個(gè)例子:
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);
我已經(jīng)myCourses根據(jù)courseid 過濾了數(shù)組。然后我為找到的每個(gè)插槽創(chuàng)建了一個(gè) forEach slots,我創(chuàng)建了一個(gè)從數(shù)組中過濾得到的對(duì)象result,course以及當(dāng)前的slot. 最后,我將這個(gè)result對(duì)象推送到名為Arrayee3001.
添加回答
舉報(bào)