midhun k midhun k - 5 months ago 7
JSON Question

how to merge array to the json to form a expected result

I have an JSON array with me:

var row={ shopId: 3, shopName: '1', address: 'abc', contactNumber: 1234 }


I have a array with me:

var data= [ { imageId: 1, shopId: 3, imageUrl: 'aaa' },
{ imageId: 2, shopId: 3, imageUrl: 'bbb' } ]


I have to merge these two and make a output like this:

var result = {
shopId: 3,
shopName: '1',
address: 'abc',
contactNumber: 1234,
image: [{
imageId: 1,
shopId: 3,
imageUrl: 'aaa'
}, {
imageId: 2,
shopId: 3,
imageUrl: 'bbb'
}]
}

Answer

Just assign your data to row.image like this:

var row = {
  shopId: 3,
  shopName: '1',
  address: 'abc',
  contactNumber: 1234
}

var data = [{
  imageId: 1,
  shopId: 3,
  imageUrl: 'aaa'
}, {
  imageId: 2,
  shopId: 3,
  imageUrl: 'bbb'
}]

var result = row.image = data;

console.log(row)