【发布时间】:2022-01-12 20:02:21
【问题描述】:
我正在尝试遍历对象数组,但我不知道如何根据对象的键将对象推送到单独的数组中。相反,我编写的代码将所有键的值推送到每个数组中。你能帮帮我吗?
const schedule =
[
{
week: 1,
day: "Sunday",
unit: 1,
challenge: "Data Not Available",
goals: [
'No Goals'
]
},
{
week: 1,
day: "Monday",
unit: 1,
challenge: "Javascript Fundamentals",
goals: [
'Complete js-fundamental challenge'
]
},{
week: 1,
day: "Tuesday",
unit: 1,
challenge: "No Data Available",
goals: [
'No Goals'
]
},
{
week: 1,
day: "Wednesday",
unit: 1,
challenge: "No Data Available",
goals: [
'Complete js-fundamental challenge'
]
},
{
week: 1,
day: "Thursday",
unit: 1,
challenge: "No Data Available",
goals: [
'No Goals'
]
},
{
week: 1,
day: "Friday",
unit: 1,
challenge: "No Data Available",
goals: [
'No Goals'
]
},
{
week: 1,
day: "Saturday",
unit: 1,
challenge: "No Data Available",
goals: [
'No Goals'
]
},
{
week: 2,
day: "Sunday",
unit: 2,
challenge: "Data Structures",
goals: [
'Re-implement the following data structures: linked list, stack, queue, set, graph, hash table, tree, binary search tree',
]
},
{
week: 2,
day: "Monday",
unit: 2,
challenge: "Data Structures",
goals: [
'Re-implement the following data structures: linked list, stack, queue, set, graph, hash table, tree, binary search tree',
]
},
{
week: 2,
day: "Tuesday",
unit: 3,
challenge: "Algorithms",
goals: [
'Complete coin sum',
'Complete n-paths'
]
},
{
week: 2,
day: "Wednesday",
unit: 2,
challenge: "No Data Available",
goals: [
'No Goals',
]
},
{
week: 2,
day: "Thursday",
unit: 2,
challenge: "No Data Available",
goals: [
'No Goals',
]
},
{
week: 2,
day: "Friday",
unit: 2,
challenge: "Data Structures",
goals: [
'Re-implement the following data structures: linked list, stack, queue, set, graph, hash table, tree, binary search tree',
]
},
{
week: 2,
day: "Saturday",
unit: 2,
challenge: "Data Structures",
goals: [
'Re-implement the following data structures: linked list, stack, queue, set, graph, hash table, tree, binary search tree',
]
},
{
week: 3,
day: "Monday",
unit: 4,
challenge: "Frontend Fundamentals",
goals: [
'Complete'
]
},
{
week: 3,
day: "Sunday",
unit: 5,
challenge: "No Data",
goals: [
'No Goals'
]
},
{
week: 3,
day: "Monday",
unit: 5,
challenge: "No Data",
goals: [
'No Goals'
]
},
{
week: 3,
day: "Tuesday",
unit: 5,
challenge: "No Data",
goals: [
'No Goals'
]
},
{
week: 3,
day: "Wednesday",
unit: 5,
challenge: "AJAX",
goals: [
'Connect calendar to google API',
'Create chatroom'
]
},
{
week: 3,
day: "Thursday",
unit: 5,
challenge: "No Data",
goals: [
'No Goals'
]
},
{
week: 3,
day: "Friday",
unit: 6,
challenge: "React",
goals: [
'Create tic tac toe',
'Reactify frontend code'
]
},
];
let week = [];
let day = [];
let unit = [];
let challenge = [];
let goals = [];
schedule.forEach((set, i) => {
for (let [key, value] of Object.entries(schedule[i])) {
if (schedule[i].week) {
week.push(value);
}
if (schedule[i].day) {
day.push(value)
}
if (schedule[i].unit) {
unit.push(value)
}
if (schedule[i].unit) {
challenge.push(value)
}
if (schedule[i].unit) {
goals.push(value)
}
}
})
这是控制台日志。
console.log(week)
console.log(day)
console.log(unit)
当我控制台记录它时,我得到了
[ 1,
'Sunday',
1,
'Data Not Available',
[ 'No Goals' ],
1,
'Monday',
1,
'Javascript Fundamentals',
[ 'Complete js-fundamental challenge' ],
1,
'Tuesday',
1,
'No Data Available',
[ 'No Goals' ],
1,
'Wednesday',
1,
'No Data Available',
[ 'Complete js-fundamental challenge' ],
1,
'Thursday',
1,
'No Data Available',
[ 'No Goals' ],
1,
'Friday',
1,
'No Data Available',
[ 'No Goals' ],
1,
'Saturday',
1,
'No Data Available',
[ 'No Goals' ],
2,
'Sunday',
2,
'Data Structures',
[ 'Re-implement the following data structures: linked list, stack, queue, set, graph, hash table, tree, binary search tree' ],
2,
'Monday',
2,
'Data Structures',
[ 'Re-implement the following data structures: linked list, stack, queue, set, graph, hash table, tree, binary search tree' ],
2,
'Tuesday',
3,
'Algorithms',
[ 'Complete coin sum', 'Complete n-paths' ],
2,
'Wednesday',
2,
'No Data Available',
[ 'No Goals' ],
2,
'Thursday',
2,
'No Data Available',
[ 'No Goals' ],
2,
'Friday',
2,
'Data Structures',
[ 'Re-implement the following data structures: linked list, stack, queue, set, graph, hash table, tree, binary search tree' ],
2,
'Saturday',
2,
'Data Structures',
[ 'Re-implement the following data structures: linked list, stack, queue, set, graph, hash table, tree, binary search tree' ],
3,
'Monday',
4,
'Frontend Fundamentals',
[ 'Complete' ],
3,
'Sunday',
5,
'No Data',
[ 'No Goals' ],
3,
'Monday',
5,
'No Data',
[ 'No Goals' ],
3,
'Tuesday',
5,
'No Data',
[ 'No Goals' ],
3,
'Wednesday',
5,
'AJAX',
[ 'Connect calendar to google API', 'Create chatroom' ],
3,
'Thursday',
5,
'No Data',
[ 'No Goals' ],
3,
'Friday',
6,
'React',
[ 'Create tic tac toe', 'Reactify frontend code' ] ]
对于每个控制台日志。我如何根据它们的键将它们推送到单独的数组中?
【问题讨论】:
-
你能展示一下预期的结果吗?至少有一部分。我试图从您的问题中得出它,但不确定您希望这些最终数组的外观如何。
-
我不明白您所说的“单独的数组”是什么意思 - 每个键只有一个数组?并且您的循环仅检查每个对象中是否存在该键(尽管您的所有对象都具有所有键)?请添加预期结果。
标签: javascript arrays javascript-objects for-of-loop