【问题标题】:How to iterate through an array of objects and push all the separate object values to separate arrays如何遍历对象数组并将所有单独的对象值推送到单独的数组
【发布时间】: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


【解决方案1】:

你的错误是这行代码

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)
    }
}

没有按照你的想法做。在这里,您在每次迭代条目时将 value 推送到数组中,因此您将得到混合值。

您应该做的是创建一个名为 valueArray 的数组数组,其中包含 weekdayunitchallengegoals 数组

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)

【讨论】:

  • 非常感谢亚历克斯!
【解决方案2】:

如果您只是想将它们组合到您创建的数组中,那么您可以这样做:

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 ]

如果这不是你想要的,请告诉我!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-11
    • 1970-01-01
    • 2015-04-20
    • 2019-03-27
    相关资源
    最近更新 更多