create array of array of object in javascript to get the desired result

57 Views Asked by At

Need a specific array structure.

arr = [];
arr1 = {'a':12,'b':11};
arr2 = {'c':12,'d':12};
arr.push(arr1);
arr.push(arr2);`

with this i got the result

(2) [Object, Object]
0:
{
 a:12,
 b:11
}
1:
{
 c:12,
 d:12
}

But I need this response

[{'a':12,'b':11},{'c':12,'d':12}]
1

There are 1 best solutions below

4
On BEST ANSWER

All current browsers have native JSON support built in. So as long as you're not dealing with prehistoric browsers like IE6/7 you can do it just as easily as that:

JSON.stringify(arr); //"[{"a":12,"b":11},{"c":12,"d":12}]"