0

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()
        });
      }
5
  • 4
    Side Note: Date is a class in javascript. Probably want to avoid naming your variables that. In general, variables should start with lowercase letters to avoid such things. Commented Sep 24, 2020 at 22:11
  • Can you add some more info? It looks like the section you copied is missing the lower portion. Commented Sep 24, 2020 at 22:12
  • 1
    What is your desired output? Commented Sep 24, 2020 at 22:13
  • You don't show calling Push() or returning the master array from it Commented Sep 24, 2020 at 22:14
  • What do you mean "key"? Some sort of an id? If so, consider an Object. Commented Sep 24, 2020 at 22:25

4 Answers 4

2
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

Sign up to request clarification or add additional context in comments.

Comments

2

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

Comments

1

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

Comments

-1
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.

1 Comment

That won't produce results that OP's loop would produce

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.