1

I have two JSON objects

 Arr1 ={Email: "[email protected]", status: "0"}
 Arr2 ={Email: "[email protected]", status: "1"}

When I try to make third array like,

Arr3 = Arr1.push( Arr2 )

It doesn't append correctly but second array is added as a list than a object. What am I missing ? I am expecting push will create results like,

{Email: "[email protected]", status: "0"},
{Email: "[email protected]", status: "1"}
Pedram
16.6k10 gold badges47 silver badges73 bronze badges
asked Feb 23, 2020 at 7:57
13
  • It's an object ! Commented Feb 23, 2020 at 8:01
  • Both are objects but push making it a list than a object. Commented Feb 23, 2020 at 8:01
  • You can use $.extend Commented Feb 23, 2020 at 8:03
  • so, you want like a new object have the above two objects as nested one? Commented Feb 23, 2020 at 8:04
  • Yes, append second to the first object Commented Feb 23, 2020 at 8:10

1 Answer 1

1

I am expecting push will create results like,

{Email: "[email protected]", status: "0"},
{Email: "[email protected]", status: "1"}

This result is not valid to me, you should use another object or array except you make it to string

let Arr1 = {
 Email: "[email protected]",
 status: "0"
};
let Arr2 = {
 Email: "[email protected]",
 status: "1"
};
let str1 = JSON.stringify(Arr1);
let str2 = JSON.stringify(Arr2);
console.log(str2, ',' + str2)

This makes no sense!


You have two way, add objects into one array, or add two object into one object

1.

let Arr1 = {
 Email: "[email protected]",
 status: "0"
};
let Arr2 = {
 Email: "[email protected]",
 status: "1"
};
var Arr3 = {Arr1, Arr2}
console.log(Arr3)

2.

let Arr1 = {
 Email: "[email protected]",
 status: "0"
};
let Arr2 = {
 Email: "[email protected]",
 status: "1"
};
let Arr3 = [];
Arr3.push(Arr1, Arr2)
console.log(Arr3)

answered Feb 23, 2020 at 8:30

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.