Created
February 15, 2013 02:05
-
-
Save MaraScott/4958088 to your computer and use it in GitHub Desktop.
Name : jsonConcat(arrayjson) - Language : Javascript, json - type : function - Platform : generic - tag : concatenation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// concat multiple json in one | |
function jsonConcat(arrayjson) { | |
"use strict"; | |
var o = {}; | |
for(var i=0; i<arrayjson.length; i++) { | |
o = concat2json(o, arrayjson[i]); | |
} | |
return o; | |
} | |
// add second obj to first | |
function concat2json(o1, o2) { | |
"use strict"; | |
for(var prop in o2) { | |
if (o2.hasOwnProperty(prop)) { | |
o1[prop] = o2[prop]; | |
} | |
} | |
return o1; | |
} | |
// example | |
var o1 = {"a":"1","b":"2"}, o2 = {"c":"3","d":"4"}, o3 = {"e":"5","f":"6"}; | |
var o = jsonConcat([o1,o2,o3]); | |
console.log(o); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment