user7784271
user7784271

Reputation:

Convert array to object and push object into array -javascript

I getting the response array data is:

var data =[{
    'id':'1','name':'sam'
}];
var data =[{
    'id':'2','name':'ram'
}];
var data =[{
    'id':'2','name':'ragu'
}];

I am getting this data into looping method . This results are my server side console only. But, my client side as only view the first array data only so, i need to convert that array data into object and push into another var = arraydata.

I tried this code:

            var arrayData =[];
            var  [objectData] = data;
            arrayData.push(objectData);

I Tried result is:

var arrayData =[{
            'id':'1','name':'sam'
        }];

but i getting single data only

my expected result is :

var arrayData =[{
        'id':'1','name':'sam'
    },{
        'id':'2','name':'ram'
    },{
        'id':'2','name':'ragu'
    }];

please give any solution solution for me!

Upvotes: 0

Views: 2268

Answers (8)

hong4rc
hong4rc

Reputation: 4103

I think your problem is var arrayData =[];, this code create arrayData is empty.

You should use var arrayData = arrayData || []; I hope it help you.

Upvotes: 1

Erick
Erick

Reputation: 1146

An array of Objects can be created by using the Spread operators

var data1 = [{
    'id':'1','name':'sam'
}];

var data2 = [{
    'id':'2','name':'ram'
}];
var data3 =[{
   'id':'2','name':'ragu'
}]; 
var data=[...data1,...data2, data3]

//console.log(data)

This will return you JSON object [{},{},{}]

Upvotes: 0

Bharath
Bharath

Reputation: 365

try this,

use ES6 spread operator and push the new array object into old array.

var data =[{
    'id':'1','name':'sam'
}];
var data1 =[{
    'id':'2','name':'ram'
}];

data.push(...data1)

Hope this helps you :)

Upvotes: 0

Narendra Jadhav
Narendra Jadhav

Reputation: 10262

ES6

You could use [].concat.apply([],[v1,va2.... so on])) to get the required result.

DEMO

var data =[{'id':'1','name':'sam'}], 
    data1 =[{'id':'2','name':'ram'}], 
    data2 =[{'id':'2','name':'ragu'}];
    
console.log([].concat.apply([],[data,data1,data2]))
.as-console-wrapper {max-height: 100% !important;top: 0;}


You can also use the Spread_syntax

DEMO

const data =[{'id':'1','name':'sam'}], 
    data1 =[{'id':'2','name':'ram'}], 
    data2 =[{'id':'2','name':'ragu'}];
    
console.log([...data,...data1,...data2])
.as-console-wrapper {max-height: 100% !important;top: 0;}

Upvotes: 1

Rohìt Jíndal
Rohìt Jíndal

Reputation: 27202

You can try Array concat() method.

var data1 =[{
    'id':'1','name':'sam'
}];
var data2 =[{
    'id':'2','name':'ram'
}];
var data3 =[{
    'id':'2','name':'ragu'
}];

var arrayData = data1.concat(data2, data3);

console.log(arrayData);

Using ES6 Destructuring assignment :

let data1 =[{
    'id':'1','name':'sam'
}];
let data2 =[{
    'id':'2','name':'ram'
}];
let data3 =[{
    'id':'2','name':'ragu'
}];

[...arrayData] = [...data1, ...data2, ...data3];

console.log(arrayData)

Upvotes: 1

Bruno Marcelino
Bruno Marcelino

Reputation: 93

you can use .forEatch()

var data = [{
    'id':'1','name':'sam'
}];

var data2 = [{
    'id':'2','name':'ram'
}];

data2.forEach(element => {
    data.push(element);
});

console.log(data);

Upvotes: 0

jyothi
jyothi

Reputation: 1

you can concat all the arrays into one::

suppose I have 3 arrays

var data =[{
'id':'1','name':'sam'

}]; var data1 =[{ 'id':'2','name':'ram' }]; var data2 =[{ 'id':'3','name':'ragu' }];

you can get the result by

var output=data.concat(data1,data)

Upvotes: 0

harvey
harvey

Reputation: 2953

You can youse .concat() as per the example below:

var data1 = [{
    'id':'1','name':'sam'
}];

var data2 = [{
    'id':'2','name':'ram'
}];

var out = [].concat(data1, data2);


// After more data, or one at a time example
var data3 = [{
    'id':'3','name':'ragu'
}];

out = out.concat(data3)

console.log(out);

-> [{
->    'id':'1','name':'sam'
-> },{
->    'id':'2','name':'ram'
-> },{
->     'id':'2','name':'ragu'
-> }];

Upvotes: 0

Related Questions