Home > Web Front-end > JS Tutorial > JS merge json object instance

JS merge json object instance

小云云
Release: 2018-01-26 09:42:52
Original
2386 people have browsed it

We have shared many articles about json objects. This article mainly introduces the method of merging json objects in JS, involving related operation skills of javascript recursive calls and json format data traversal. Friends who need it can refer to it. I hope it can help everyone. .

1. Question:

How to merge json objects


var a ={"a":"1","b":"2"}
var b ={"c":"3","d":"4","e":"5"}
Copy after login

Want to get the result:


var c ={"a":"1","b":"2","c":"3","d":"4","e":"5"}
Copy after login

2. Implementation code:


##

<script>
function extend(des, src, override){
  if(src instanceof Array){
    for(var i = 0, len = src.length; i < len; i++)
       extend(des, src[i], override);
  }
  for( var i in src){
    if(override || !(i in des)){
      des[i] = src[i];
    }
  }
  return des;
}
var a ={&quot;a&quot;:&quot;1&quot;,&quot;b&quot;:&quot;2&quot;}
var b ={&quot;c&quot;:&quot;3&quot;,&quot;d&quot;:&quot;4&quot;,&quot;e&quot;:&quot;5&quot;}
var c = extend({}, [a,b]);
console.log(c);
</script>
Copy after login
Run results:

Related recommendations:


Manipulate JSON objects in Javascript and add a simple implementation of deletion and modification

Detailed explanation of jquery traversal and adding Json object code examples

Detailed explanation of JavaScript json object and array conversion simple implementation method example

Detailed explanation of JS operation of converting xml object into Json object

js Analysis of the method of converting json string into json object

The above is the detailed content of JS merge json object instance. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template