把两个一模一样的json合并成为一个
Posted 我爱大西瓜
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了把两个一模一样的json合并成为一个相关的知识,希望对你有一定的参考价值。
1 <!DOCTYPE html> 2 <html> 3 <head> 4 <meta charset="utf-8" /> 5 <meta name="viewport" content="width=device-width, initial-scale=1"> 6 <title></title> 7 <script src="https://cdn.staticfile.org/jquery/3.3.1/jquery.js"></script> 8 </head> 9 <body> 10 <script type="text/javascript"> 11 var jsonObj1 = { 12 0: { 13 tiwaishijian: "2018-09-09", 14 petage: "1", 15 gotohometime: "1539435900", 16 orderid: "405" 17 }, 18 1: { 19 tiwaishijian: "2018-09-03", 20 petage: "7", 21 gotohometime: "1541001600", 22 orderid: "404" 23 }, 24 2: { 25 tiwaishijian: "2018-09-03", 26 petage: "7", 27 gotohometime: "1538323200", 28 orderid: "148" 29 }, 30 3: { 31 tiwaishijian: "2018-09-03", 32 petage: "7", 33 gotohometime: "1538323200", 34 orderid: "148" 35 } 36 }; 37 38 var jsonObj2 = { 39 0: { 40 tiwaishijian: "2018-09-09", 41 petage: "1", 42 gotohometime: "1539435900", 43 orderid: "405" 44 }, 45 1: { 46 tiwaishijian: "2018-09-03", 47 petage: "7", 48 gotohometime: "1541001600", 49 orderid: "404" 50 }, 51 2: { 52 tiwaishijian: "2018-09-03", 53 petage: "7", 54 gotohometime: "1538323200", 55 orderid: "148" 56 }, 57 3: { 58 tiwaishijian: "2018-09-03", 59 petage: "7", 60 gotohometime: "1538323200", 61 orderid: "148" 62 } 63 }; 64 65 let mergeJsonObject = function(jsonbject1, jsonbject2) { 66 let resultArrObject = [], 67 resultJsonObject = {}; 68 for (let attr in jsonbject1) { 69 resultArrObject.push(jsonbject1[attr]); 70 } 71 for (let attr in jsonbject2) { 72 resultArrObject.push(jsonbject2[attr]); 73 } 74 for (let i = 0; i < resultArrObject.length; i++) { 75 resultJsonObject[i] = resultArrObject[i]; 76 } 77 return resultJsonObject; 78 }; 79 console.log(mergeJsonObject(mergeJsonObject(jsonObj1, jsonObj2), jsonObj2)); 80 </script> 81 </body> 82 </html>
以上是关于把两个一模一样的json合并成为一个的主要内容,如果未能解决你的问题,请参考以下文章