同上一篇,但只適用於鍵名不同的json
阿新 • • 發佈:2018-10-30
asc merge code htm 不同的 tex ttr idt har
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 11 <script type="text/javascript"> 12 var jsonObj1 = { 13 a: 2 14 }; 15 var jsonObj2 = { 16 b: 2, 17 c: 3, 18 d: 4 19 };20 21 let mergeJsonObject = function(jsonbject1, jsonbject2) { 22 let resultJsonObject = {}; 23 for (let attr in jsonbject1) { 24 resultJsonObject[attr] = jsonbject1[attr]; 25 } 26 for (let attr in jsonbject2) {27 resultJsonObject[attr] = jsonbject2[attr]; 28 } 29 return resultJsonObject; 30 }; 31 console.log(mergeJsonObject(jsonObj1, jsonObj2)) 32 </script> 33 </body> 34 </html>
同上一篇,但只適用於鍵名不同的json