0

I'm a beginner in Javascript so please exuse this probably dumb question. I want to merge two json files based on unique object id.

Number one look like this:

"features": [{
      "id": "3876802",
      "properties": {
        "name": "some name",
        "facts": "some facts"}},
      {"id": "3876803",
      "properties": {"name":"another name"...}}...]

Number Two looks like this:

"features": [{
          "id": "3876803",
          "properties": {
          "description": "some description", 
          "website": "afancywebsite"}},
         {"id": "3876803",
          "properties": {...}}]

The Elements in the second Json are not in the same order and not all elements of the first file exist in the second.

The Result should look like this:

"features": [{
          "id": "3876802",
          "properties": {
            "name": "some name",
            "facts": "some facts"}},{
          "id": "3876803",
          "properties": {
            "name":"another name",
            "description": "some description", 
            "website": "afancywebsite"}}]

I started coding this but I have no idea how to get it working...

 for(var i in json1.features){
        for (var z in json2.features){
        if (json1.features[i].id===json2.features[z].id){
            json1.feature[i].properties = json2.features[z].properties}}}
hno2
  • 97
  • 2
  • 16

1 Answers1

1

This will do the job:

    var features =  [
    {"id": "3876802",
        "properties": {
            "name": "some name",
            "facts": "some facts"}
    },
    {"id": "3876803",
        "properties": {
            "name":"another name"
        }
    }
    ];


    var features2 = [{
        "id": "3876803",
        "properties": {
            "description": "some description", 
            "website": "afancywebsite"
            }
        }
    ];


features.map(function(feature){
    var matchedArray = features2.filter(function(feature2){
        return feature2.id === feature.id;
    });
    if(matchedArray && matchedArray[0]){
        for(var attr in matchedArray[0].properties){
            feature.properties[attr] = matchedArray[0].properties[attr];
        }
    }
});

We start by using Array.map() to run through the 'features' array, one by one.

Then we use Array.filter() on the features2 array which gives us an array containing the only object in features2 (matched[0]) which has the same id as feature.id.

If there's a match, then we run through the 'properties' in the features2 object using a 'for in' loop and copy them to the 'feature' object.


If you want to get advanced info about this check out this stackoverflow question: How can I merge properties of two JavaScript objects dynamically?. For example, if you're writing bullet-proof javascript you should use 'hasOwnProperty' in a for in loop. You may also want to guard against properties in 'features2' overwriting a property with the same name in 'features'.


However if you would like to keep your code more or less as it was this also works:

for(var i in features){
    for (var z in features2){
        if (features[i].id===features2[z].id){
            for(var attr in features2[z].properties){
                features[i].properties[attr] = features2[z].properties[attr];
            }
        }
    }
}
Community
  • 1
  • 1
mwarren
  • 2,409
  • 1
  • 22
  • 28