Valador1988
Valador1988

Reputation: 51

Push multiple arrays with keys into single array

I working on a Daily Planner/Organiser. I have a few seperate arrays with event information such as Event, Date, Venue, Start Time, End Time etc. All the information for an associated event is in these seperate arrays. How can I pull them all together and push them into a single array (MasterArray) with key pair? Is that possible? I was trying the below function but I can't get it to work

    var Event = ["class","party"];
    var Date = ["2/3/2020","16/5/2020"];
    var StartTime = ["9","11"];
    var EndTime = ["10","15"];
    var ApptVenue = ["Classroom","Arcade"];
    

    function Push() {
      var MasterArrayLen = Event.length;
      var MasterArray= [];
      for (let i = 0; i < MasterArrayLen; i++) {
        MasterArray.push({
          "Event": Event .shift(),
          "Date": Date .shift(),
          "Start": StartTime .shift(),
          "End": EndTime .shift()
        });
      }

Upvotes: 0

Views: 5453

Answers (4)

charlietfl
charlietfl

Reputation: 171669

Instead of using indivudual variables, put those arrays into one object with same keys that you want in the individual objects you want to create.

Then you can use map() on one of the arrays and iterate the master object entries using the index of map() to get the appropriate values.

Using this approach you don't need to know the individual property names that exist in the master object. Adding or removing a property in the master object doesn't require any changes to the processing code

const data = {
  event: ["class", "party"],
  date: ["2/3/2020", "16/5/2020"],
  startTime: ["9", "11"],
  endTime: ["10", "15"],
  apptVenue: ["Classroom", "Arcade"]

}


function Push() {
  return data.event.map((_, i) => {
    return Object.entries(data).reduce((a, [k, arr]) => (a[k] = arr[i], a), {})
  })

}

console.log(Push())
.as-console-wrapper {   max-height: 100%!important;top:0;}

Upvotes: 2

pilchard
pilchard

Reputation: 12920

This is a good situation for reduce()

The reduce() method executes a reducer function (that you provide) on each element of the array, resulting in single output value.

Array.prototype.reduce()

const events = ["class", "party"]
const dates = ["2/3/2020", "16/5/2020"]
const startTimes = ["9", "11"]
const endTimes = ["10", "15"]
const apptVenues = ["Classroom", "Arcade"]

function push () {

  const masterArray = events.reduce((acc, event, i) => {
    const master = { 
      event: events[i], 
      date: dates[i], 
      startTime: startTimes[i], 
      endTime: endTimes[i], 
      apptVenue: apptVenues[i] 
    }
    acc = acc.concat(master);
    return acc;
  }, []);

  return masterArray
}

console.log(push())

Upvotes: 1

user2258152
user2258152

Reputation: 675

const events = ["class", "party"]
const dates = ["2/3/2020", "16/5/2020"]
const startTimes = ["9", "11"]
const endTimes = ["10", "15"]
const apptVenues = ["Classroom", "Arcade"]

function push () {
  var masterArray = []
  for (let i = 0; i < events.length; i++) {
    const master = { event: events[i], date: dates[i], startTime: startTimes[i], endTime: endTimes[i], apptVenue: apptVenues[i] }
    masterArray.push(master)
  }
  return masterArray
}

console.log(push())

The result:

[ { event: 'class',
    date: '2/3/2020',
    startTime: '9',
    endTime: '10',
    apptVenue: 'Classroom' },
  { event: 'party',
    date: '16/5/2020',
    startTime: '11',
    endTime: '15',
    apptVenue: 'Arcade' } ]

You should use camelCase when declaring variables and functions

Upvotes: 2

Tony Drummond
Tony Drummond

Reputation: 375

var Event = ["class", "party"];
var Date = ["2/3/2020", "16/5/2020"];
var StartTime = ["9", "11"];
var EndTime = ["10", "15"];
var ApptVenue = ["Classroom", "Arcade"];

console.log({
    Event: [...Event],
    Date: [...Date],
    Start: [...StartTime],
    End: [...EndTime],
});

Use spread operator...very simple this way.

One more comment - your "Date" variable should not be structured that way. Date is actually a JavaScript object. Make variables in camelCase instead.

Upvotes: -1

Related Questions