combine two json arrays in javascript - Stack Overflow

I have two json arrays like,array1 = [{"quantity":"5","detailed_product_id&quo

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 bine 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.

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 bine 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.

Share Improve this question asked Mar 22, 2017 at 9:21 AryaArya 5042 gold badges9 silver badges31 bronze badges 2
  • 4 Possible duplicate of Merge two json/javascript arrays in to one array – Kakashi Hatake Commented Mar 22, 2017 at 9:22
  • How do you tell if you have dup array items? Or do you care? – unflores Commented Mar 22, 2017 at 9:24
Add a ment  | 

4 Answers 4

Reset to default 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));

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 bineJsonArray = [...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"}]

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));

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>

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744963813a4603550.html

相关推荐

  • combine two json arrays in javascript - Stack Overflow

    I have two json arrays like,array1 = [{"quantity":"5","detailed_product_id&quo

    1天前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信