Search code examples
javascriptjqueryarraysarrayofarrays

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


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}]

Solution

  • 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}]"