Home > Enterprise >  How to iterate through an array of objects and push all the separate object values to separate array
How to iterate through an array of objects and push all the separate object values to separate array

Time:01-13

I am trying to iterate through an array of objects and I can't figure out how to push the objects into separate arrays based off their keys. Instead the code that I wrote pushes all the key's values into each array. Could you please help me?

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

Here are the console logs.

    console.log(week)
    console.log(day)
    console.log(unit)

When I console log it I get

[ 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' ] ]

For each of the console logs. How would I push them into separate arrays based off their keys?

CodePudding user response:

If you mean just to group them together into those arrays you created so you could do:

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

This will provide you outputs like this:

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 ]

If this is not what you want, please, let me know!

CodePudding user response:

Your error is that this lines of code

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

are not doing what you think. Here you are pushing the value in the arrays on every iteration of the entries, so you will have mixed values as a result.

What you should do is to create an array of arrays called valueArray which will have the week, day, unit, challenge, and goals arrays inside

const valueArray = [week, day, unit, challenge, goals];

now, replace your entries loop with a forEach:

Object.values(schedule[i]).forEach((value, i) => {
    valueArray[i].push(value);
});

Try it here

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)

  •  Tags:  
  • Related