1

I have the following two objects:

var objectA = {
    "a": [
        {
            "col": 1,
            "row": 1,
            "pnl": 1.0
        },
        {
            "col": 1,
            "row": 2,
            "pnl": 2.0
        },
    ]
};

var objectB = {
    "a": [
        {
            "col": 1,
            "row": 1,
            "sharpe": 1.5
        },
        {
            "col": 1,
            "row": 2,
            "sharpe": 2.5
        },
    ]
};

and I'd like to get:

var objectC = {
    "a": [
        {
            "col": 1,
            "row": 1,
            "pnl": 1.0,
            "sharpe": 1.5
        },
        {
            "col": 1,
            "row": 2,
            "pnl": 2.0,
            "sharpe": 2.5
        },
    ]
};
julianstark999
  • 3,450
  • 1
  • 27
  • 41
SkyWalker
  • 13,729
  • 18
  • 91
  • 187
  • 3
    Just so you know, there's a common misconception about JSON vs. objects. JSON is just a textual format. What you have there is just objects and arrays. I've edited your question to reflect this. That being said, have you tried anything yourself to solve this? Where did you get stuck? – Mike Cluck Nov 15 '17 at 14:58

3 Answers3

3

You could use a hash table for reference to the same key and col/row items. Then update the object or assign the new object if no hash is available.

var objectA = { a: [{ col: 1, row: 1, pnl: 1.0 }, { col: 1, row: 2, pnl: 2.0 }] },
    objectB = { a: [{ col: 1, row: 1, sharpe: 1.5 }, { col: 1, row: 2, sharpe: 2.5 }] },
    hash = Object.create(null);

Object.keys(objectA).forEach(function (k) {
    hash[k] = hash[k] || {};
    objectA[k].forEach(function (o) {
        var key = ['col', 'row'].map(function (l) { return o[l]; }).join('|');
        hash[k][key] = o;
    });
});

Object.keys(objectB).forEach(function (k) {
    if (!hash[k]) {
        objectA[k] = objectB[k];
        return;
    }
    objectB[k].forEach(function (o) {
        var key = ['col', 'row'].map(function (l) { return o[l]; }).join('|');
        if (hash[k][key]) {
            Object.keys(o).forEach(function (l) {
                hash[k][key][l] = o[l];
            });
        } else {
            objectA[k].push(o);
        }
    });
});

console.log(objectA);
.as-console-wrapper { max-height: 100% !important; top: 0; }
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
1

I would use lodash’s _.merge method in this case.

This method is like _.assign except that it recursively merges own and inherited enumerable string keyed properties of source objects into the destination object. Source properties that resolve to undefined are skipped if a destination value exists. Array and plain object properties are merged recursively. Other objects and value types are overridden by assignment. Source objects are applied from left to right. Subsequent sources overwrite property assignments of previous sources.

var object = {
  'a': [{ 'b': 2 }, { 'd': 4 }]
};

var other = {
  'a': [{ 'c': 3 }, { 'e': 5 }]
};

_.merge(object, other);
// => { 'a': [{ 'b': 2, 'c': 3 }, { 'd': 4, 'e': 5 }] }

https://lodash.com/docs/4.17.4#merge

Or if you need it to not be part of a library, then look at the lodash’s implementation.

Misha Reyzlin
  • 13,736
  • 4
  • 54
  • 63
0

Just use Object.assign to merge objects:

var json1 = {"a":[ 
               {
                 "col": 1, 
                 "row": 1,
                 "pnl": 1.0
               },
               {
                 "col": 1, 
                 "row": 2,
                 "pnl": 2.0
               },  
           ]};

var json2 = {"a":[ 
               {
                 "col": 1, 
                 "row": 1,
                 "sharpe": 1.5
               },
               {
                 "col": 1, 
                 "row": 2,
                 "sharpe": 2.5
               },  
           ]};
           
 var objectC = { a: json1.a.map((el, index) => Object.assign({}, el, json2.a[index])) };
 console.log(objectC);
Faly
  • 13,291
  • 2
  • 19
  • 37
  • I have a feeling that they don't want to specify `json1.a` directly. That they want a function which will do this for all fields automatically. I could be wrong though. – Mike Cluck Nov 15 '17 at 14:59
  • @MikeC yes I'd like a generic solution otherwise changing the structure of the json (that comes from a Scala backend) might break it. – SkyWalker Nov 15 '17 at 15:01
  • I actually ended using this one to avoid too complex solutions or inflating my dependency footprint. – SkyWalker Nov 15 '17 at 20:56