-2

I have two json arrays like,

array1 = [{"quantity":"5","detailed_product_id":"1015","detailed_category_id":"9"}]

and

array2 = [{"quantity":"2","detailed_product_id":"1003","detailed_category_id":"9"}]

I have to combine these two arrays into one array like,

resultarray = [{"quantity":"5","detailed_product_id":"1015","detailed_category_id":"9"},{"quantity":"2","detailed_product_id":"1003","detailed_category_id":"9"}]

Please help me.

Arya
  • 504
  • 2
  • 8
  • 31
  • 4
    Possible duplicate of [Merge two json/javascript arrays in to one array](http://stackoverflow.com/questions/10384845/merge-two-json-javascript-arrays-in-to-one-array) – CodeMonkey Mar 22 '17 at 09:22
  • How do you tell if you have dup array items? Or do you care? – unflores Mar 22 '17 at 09:24

4 Answers4

4

array1 = [{"quantity":"5","detailed_product_id":"1015","detailed_category_id":"9"}]
array2 = [{"quantity":"2","detailed_product_id":"1003","detailed_category_id":"9"}]
console.log(array1.concat(array2));
baao
  • 71,625
  • 17
  • 143
  • 203
  • It's not working. Getting result like, [{"quantity":"2","detailed_product_id":"1003","detailed_category_id":"9"}][{"quantity":"5","detailed_product_id":"1003","detailed_category_id":"9"}] – Arya Mar 22 '17 at 09:47
  • Above returns exactly what you asked for @Arya, log the resulting array instead of the two orriginal ones – baao Mar 22 '17 at 09:48
  • after concatenating two arrays I am getting the above result @baao – Arya Mar 22 '17 at 09:59
  • Just click on "run code snippet". The resulting array is exactly as in your question. @Arya – baao Mar 22 '17 at 10:01
3

You can do this using Es 6 new feature:

array1=[{"quantity":"5","detailed_product_id":"1015","detailed_category_id":"9"}]

array2 = [{"quantity":"2","detailed_product_id":"1003","detailed_category_id":"9"}]

var combineJsonArray = [...array1, ...array2 ];

//output should be like this [ {"quantity":"5","detailed_product_id":"1015","detailed_category_id":"9"},
  {"quantity":"2","detailed_product_id":"1003","detailed_category_id":"9"}]

Or You can put extra string or anything between two JSON array:

var array3= [...array1,"test", ...array2];

// output should  be like this :  [ {"quantity":"5","detailed_product_id":"1015","detailed_category_id":"9"},"test",
  {"quantity":"2","detailed_product_id":"1003","detailed_category_id":"9"}]
Hoque MD Zahidul
  • 10,560
  • 2
  • 37
  • 40
0

Use the concat function.

var resultarray = array1.concat(array2);

Result shown below:

array1 = [{
  "quantity": "5",
  "detailed_product_id": "1015",
  "detailed_category_id": "9"
}];

array2 = [{
  "quantity": "2",
  "detailed_product_id": "1003",
  "detailed_category_id": "9"
}];

console.log(array1.concat(array2));
CodeMonkey
  • 2,828
  • 1
  • 23
  • 32
0

Try array.concat for this.

<!DOCTYPE html>
<html>

<head>
    <script>
        var json1 = [{
            "quantity": "5",
            "detailed_product_id": "1015",
            "detailed_category_id": "9"
        }];
        var json2 = [{
            "quantity": "2",
            "detailed_product_id": "1003",
            "detailed_category_id": "9"
        }]
        var json3 = json2.concat(json1);
        console.log(json3)
    </script>
</head>

</html>
Nitheesh
  • 19,238
  • 3
  • 22
  • 49