Home > Mobile >  How to push multiple json object in array and make array of object in node js
How to push multiple json object in array and make array of object in node js

Time:08-09

I am new to node js and I was trying to make array of object I searched and found no help as I tried this but its not helping. I am trying to push json object but its showing me one data at a time.

so there is one code where i wrote and I am getting this as a result when i do console.log( result);

{data1:{all object here}}
{data2:{all object here}}
... // it can be more then 2

So I wrote code like this

const arrayObject=[];
arrayObject.push(result);

so my op look like this when I do console.log(arrayObject)

[
{data1:{all object here}}
]

[
{data1:{all object here}}
]

[
{}
]

as my expected op is like this

[
{data1:{all object here}},
{data2:{all object here}},
// more if there is more result not just 2
]

CodePudding user response:

If you have 2 results,as easy as it sounds you can use const arr=[result1,result2]

CodePudding user response:

As it looks like your API results are arrays, and you need them collected in one array, you can use spread syntax:

const arrayObject = [];
// ... get result ...
arrayObject.push(...result);
  • Related