0

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.

4
  • you must define what do you mean by exist. What are the values that you use to compare? Commented Feb 5, 2016 at 2:52
  • Thank you for the reply. I removed the if section as it works fine. I am just having a hard time adding this mixed array into the NAMES variable. Commented Feb 5, 2016 at 2:55
  • what does "return name : [input1, input2, input3], ;" mean? Commented Feb 5, 2016 at 3:05
  • First, fix your syntax errors. Commented Feb 5, 2016 at 4:19

1 Answer 1

2

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!

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

Comments

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.