1

I want to combine two arrays containing json objects but keep the duplicate keys by prepending the keys with some text. In the example below the data from the object json2 is overwriting the object json1 because they have the same keys but I want to keep the data from the 1st object as well. Thanks.

var json1 = [
  {
    "Column1": "json1",
    "Column2": "json1",
  },
  {
    "Column1": "json1",
    "Column2": "json1",
  }
];
var json2 = [
  {
    "Column1": "json2",
    "Column2": "json2",
  },
  {
    "Column1": "json2",
    "Column2": "json2",
  }
];

console.log(angular.extend(json1, json2));

is returning

[
  {
    "Column1": "json2",
    "Column2": "json2",
  },
  {
    "Column1": "json2",
    "Column2": "json2",
  }
];

but I want

[
  {
    "json1Column1": "json1",
    "json1Column2": "json1",
    "json2Column1": "json2",
    "json2Column2": "json2",
  },
  {
    "json1Column1": "json1",
    "json1Column2": "json1",
    "json2Column1": "json2",
    "json2Column2": "json2",
  }
];
3
  • For that kind of combining you'll have to do it with a small bit of custom code. Commented Feb 9, 2016 at 16:13
  • 1
    Perhaps prepending 'json1' to all of the members of json1, and doing the same for json2, before merging them? Commented Feb 9, 2016 at 16:14
  • They are not JSON objects. Those are javascript arrays. JSON is a subset of Javascript literals serialized to a string. Commented Feb 9, 2016 at 16:52

2 Answers 2

2

The only thing I can think is to create a function to add prefix to objects:

function addPrefix(target, prefix){
    var newObj = false;
    if(target.constructor === Array){
        newObj = [];
        for(var i = 0 ; i< target.length; i++){
            item = target[i];
            var itemObj = {}; 
            for(var key in item){
                itemObj[prefix+key] = item[key]; 
            }
            newObj.push(itemObj);         
        }       
    }else{
        newObj = {};
        for(var key in target){
                newObj[prefix+key] = target[key]; 
        }
    }
     return newObj;
}

After that avoid using angular.extend, it doesn't support deep properties copy. So that's the reason why the properties from the second object are copied into the first. What you need is angular.merge, so you code would be after implementing the previous function:

var nJson1=addPrefix(json1,"json1");
var nJson2=addPrefix(json2,"json2");
var merged = angular.merge({},nJson1,nJson2);
Sign up to request clarification or add additional context in comments.

Comments

1

A proposal with some iterating over the keys and over the items.

var obj1 = [{ Column1: "json11", Column2: "json11", }, { Column1: "json12", Column2: "json12", }],
    obj2 = [{ Column1: "json21", Column2: "json21", }, { Column1: "json22", Column2: "json22", }],
    result = function (data) {
        var r = [];
        Object.keys(data).forEach(function (k) {
            data[k].forEach(function (a, i) {
                r[i] = r[i] || {};
                Object.keys(a).forEach(function (l) {
                    r[i][k + l] = a[l];
                });
            });
        });
        return r;
    }({ json1: obj1, json2: obj2 });

document.write('<pre>' + JSON.stringify(result, 0, 4) + '</pre>');

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.