1

I have two objects:

var arr= [ 0: { selectedAns: "test answer 1", id: "28", status: "answered" } ] 

and:

var answerList = [ 0: { selectedAns: "test answer 2 ques 5", id: "27", status: "answered" }, 1: { selectedAns: "test answer 3 ques 6", id: "29", status: "answered"} ] 

When I do this:

arr1.push(arr); arr1.push(answerList); 

I am getting this result:

var arr1=[0:{ 0:{selectedAns: "test answer 2 ques 5", id: "27", status: "answered"} } 1:{ 0:{selectedAns: "test answer 1", id: "28", status: "answered"} 1:{selectedAns: "test answer 3 ques 6", id: "29", status: "answered"} }] 

But I want something like this:

 var arr1=[ 0:{selectedAns: "test answer 2 ques 5", id: "27", status: "answered"} 1:{selectedAns: "test answer 1", id: "28", status: "answered"} 2:{selectedAns: "test answer 3 ques 6", id: "29", status: "answered"} ] 

What am I doing wrong? Thanks.

Note: I am removing duplicates from objects before merging two objects, even if any key has duplicate value like here in status key. Still, I need to merge both the objects. Per my requirement only the id key needs to be unique, so I need a way to merge the two objects as above.

4
  • Get rid of all the 0: , 1: etc syntax shown...none of that is valid syntax. An array of objects looks like [{},{},{}] Commented Dec 2, 2017 at 19:52
  • Looks like you have two arrays, arr and answerList. Then you try to address something called arr1. What is it? Commented Dec 2, 2017 at 19:58
  • arr1 is a variable in which i am trying to store the result after merging both arr and answerList .@Roamer-1888 Commented Dec 2, 2017 at 20:07
  • why the array(s). Why not simply deal with native js objects? Commented Dec 2, 2017 at 20:30

2 Answers 2

2

You could use either Array#concat, which need an assignment of the new array.

var arr = [{ selectedAns: "test answer 1", id: "28", status: "answered" }], answerList = [{ selectedAns: "test answer 2 ques 5", id: "27", status: "answered" }, { selectedAns: "test answer 3 ques 6", id: "29", status: "answered" }]; arr = arr.concat(answerList); console.log(arr);
.as-console-wrapper { max-height: 100% !important; top: 0; }

Or use Array#push with Function#apply for taking an array as parameter.

var arr = [{ selectedAns: "test answer 1", id: "28", status: "answered" }], answerList = [{ selectedAns: "test answer 2 ques 5", id: "27", status: "answered" }, { selectedAns: "test answer 3 ques 6", id: "29", status: "answered" }]; Array.prototype.push.apply(arr, answerList); console.log(arr);
.as-console-wrapper { max-height: 100% !important; top: 0; }

With ES6 you could push the values with spread syntax ... which takes an array as parameters.

var arr = [{ selectedAns: "test answer 1", id: "28", status: "answered" }], answerList = [{ selectedAns: "test answer 2 ques 5", id: "27", status: "answered" }, { selectedAns: "test answer 3 ques 6", id: "29", status: "answered" }]; arr.push(...answerList); console.log(arr);
.as-console-wrapper { max-height: 100% !important; top: 0; }

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

3 Comments

@M0ns1f, the indices are not valid parts of an array in javascript. i assume this is only a part of displaying arrays in the console.
Well if he want to use the indices, see my answer and give me your opinion ^^'
@NinaScholz thanks for responding so quick... you saved my life, arr = arr.concat(answerList); did the work for me thank again...
0

Well using javascript you can push the answerList array to the arr1 using for loop see this function :

var arr1 = []; var arr={0:{selectedAns: "test answer 1", id: "28", status: "answered"}}; var answerList={0:{selectedAns: "test answer 2 ques 5", id: "27", status: "answered"},1:{selectedAns: "test answer 3 ques 6", id: "29", status: "answered"}}; arr1.push(arr);//push first array pashingarray(answerList); //function to push an array function pashingarray(arrtobepush) { var lenghtar = 0; for(var key in arrtobepush) { if(arrtobepush.hasOwnProperty(key)){ lenghtar++;//get the lenght of the array } } for(var i = 0;i<lenghtar;i++)//loop the array to push the content { var keytopush = i+1; var arraytopush = {[keytopush]:arrtobepush[i]}; arr1.push(arraytopush); } } console.log(arr1);
.as-console-wrapper { max-height: 100% !important; top: 0; }

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.