![](/img/trans.png)
[英]How to place these 2 arrays inside separate objects and push to the main array in JS?
[英]How to iterate through an array of objects and push all the separate object values to separate arrays
我正在尝试遍历对象数组,但我不知道如何根据它们的键将对象推送到单独的 arrays 中。 相反,我编写的代码将所有键的值推送到每个数组中。 请你帮助我好吗?
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' ] ]
对于每个控制台日志。 我将如何根据他们的密钥将它们推入单独的 arrays ?
如果您只想将它们组合到您创建的 arrays 中,那么您可以这样做:
const week = [];
const day = [];
const unit = [];
const challenge = [];
const goals = [];
schedule.forEach((set) => {
week.push(set.week);
day.push(set.day);
unit.push(set.unit);
challenge.push(set.challenge);
goals.push(set.goals);
});
这将为您提供如下输出:
console.log(week); // [ 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3 ]
console.log(day); // [ 'Sunday', 'Monday', 'Tuesday', 'Wednesday', ..., 'Friday']
console.log(unit); // [ 1, 1, 1, 1, 1, 1, 1, 2, 2, 3, 2, 2, 2, 2, 4, 5, 5, 5, 5, 5, 6 ]
如果这不是你想要的,请告诉我!
你的错误是这行代码
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)
}
}
没有按照你的想法做。 在这里,您将在每次迭代条目时推送 arrays 中的value
,因此您将得到混合值。
您应该做的是创建一个名为valueArray
的 arrays 数组,其中包含week
、 day
、 unit
、 challenge
和goals
arrays
const valueArray = [week, day, unit, challenge, goals];
现在,用 forEach 替换您的条目循环:
Object.values(schedule[i]).forEach((value, i) => {
valueArray[i].push(value);
});
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' ] }, ]; const week = []; const day = []; const unit = []; const challenge = []; const goals = []; const valueArray = [week, day, unit, challenge, goals]; schedule.forEach(set => { Object.values(set).forEach((value, i) => { valueArray[i].push(value); }); }); console.log(week) console.log(day) console.log(unit) console.log(challenge) console.log(goals)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.