HEYHEY
HEYHEY

Reputation: 533

js adding mixed multidimentional array into a variable

So, I have following js to check if object exists in the NAMES variable. If it does not, I want to push the whole object array into the variable as below:

var NAMES = {};

function NAME_INFO(name,input1,input2,input3){ 
    return  name : [input1, input2, input3], ;   
}
...
NAMES.push(NAME_INFO(my_name,first,middle,last));   //some variables that I have.   

The ending result that I am trying to get is as follow:

 var NAMES = {
   '185' : ['ryan', 'some', 'last'],
   '15'  : ['mike', 'middle', 'Mcdonald'],
   '122' : ['emily','else', 'Another']
 }; 

I am not sure if this is the right setup for it. I am not sure how to make the function correctly.

Could someone point me to the right direction? Thanks.

Upvotes: 0

Views: 32

Answers (1)

Joshua H
Joshua H

Reputation: 774

First of all, you can not return a map with that function. You need to do this

function NAME_INFO(name,input1,input2,input3){ 
    var newMap = {};
    newMap[name] = [input1, input2, input3];
    return newMap ;   
}

Secondly, you can not push an object to a MAP object. Your NAMES should be define as array i.e. var NAMES = []

Your final result will look like

var NAMES = [
   {'185' : ['ryan', 'some', 'last']},
   {'15'  : ['mike', 'middle', 'Mcdonald']},
   {'122' : ['emily','else', 'Another']}
]; 

Hope it helps!

Upvotes: 2

Related Questions