1

I have 2 different object arrays which i would like to merge according to the "id" value.

So if my first array looks like this:

[
  {
   "id": "75318408571184",
   "component": "textInput",
   "index": 0,
   "label": "Text",
   "description": "description",
   "placeholder": "placeholder",
   },
  {
  "id": "9463537670672",
  "component": "textArea",
  "index": 1,
  "label": "Paragraph",
  "description": "description",
  "placeholder": "placeholder"
  }

]

and my second one looks something like this:

[
  {
   "id": "75318408571184",
   "value": "value1"
   },
  {
  "id": "9463537670672",
  "value": "value2"
  }

]

i would like to get this array of objects:

 [
  {
   "id": "75318408571184",
   "component": "textInput",
   "index": 0,
   "label": "Text",
   "description": "description",
   "placeholder": "placeholder",
   "value": "value1"
   },
  {
  "id": "9463537670672",
  "component": "textArea",
  "index": 1,
  "label": "Paragraph",
  "description": "description",
  "placeholder": "placeholder",
   "value": "value2"
  }

]

is there a neat way to do this in angular or javascript without looping through the array?

3 Answers3

2

try this:

var result = firstArray.map(function(item) {
    var second = secondArray.find(function(i) {
        return item.id === i.id;
    });
    return second ? Object.assign(item, second) : item;
});
console.log(result);

Array.prototype.map() applies function in argument on each item of firstArray and returns new array with modified values.

Object.assign() is function which copies properties of second object to the item object in the code above.

madox2
  • 39,489
  • 13
  • 88
  • 91
2

In plain Javascript, you can use Array.prototype.forEach() and Array.prototype.some()

var obj1 = [{ "id": "75318408571184", "component": "textInput", "index": 0, "label": "Text", "description": "description", "placeholder": "placeholder", }, { "id": "9463537670672", "component": "textArea", "index": 1, "label": "Paragraph", "description": "description", "placeholder": "placeholder" }],
    obj2 = [{ "id": "75318408571184", "value": "value1" }, { "id": "9463537670672", "value": "value2" }];

obj2.forEach(function (a) {
    obj1.some(function (b) {
        if (a.id === b.id) {
            b.value = a.value;
            return true;
        }
    });
});
document.write('<pre>' + JSON.stringify(obj1, 0, 4) + '</pre>');

Another possibility is to build a hash table temp first and then manipulate the item directly.

var obj1 = [{ "id": "75318408571184", "component": "textInput", "index": 0, "label": "Text", "description": "description", "placeholder": "placeholder", }, { "id": "9463537670672", "component": "textArea", "index": 1, "label": "Paragraph", "description": "description", "placeholder": "placeholder" }],
    obj2 = [{ "id": "75318408571184", "value": "value1" }, { "id": "9463537670672", "value": "value2" }],
    temp = {};

obj1.forEach(function (a, i) {
    temp[a.id] = i;
});
obj2.forEach(function (a) {
    obj1[temp[a.id]].value = a.value;
});
document.write('<pre>' + JSON.stringify(obj1, 0, 4) + '</pre>');
Nina Scholz
  • 323,592
  • 20
  • 270
  • 324
2

The above answers are already good. But if you want to look at some other libraries you can have a look at this . loadash merge

var object = {
  'fruits': ['apple'],
  'vegetables': ['beet']
};

var other = {
 'fruits': ['banana'],
 'vegetables': ['carrot']
};

_.merge(object, other, function(a, b) {
  if (_.isArray(a)) {
    return a.concat(b);
 }
});

// → { 'fruits': ['apple', 'banana'], 'vegetables': ['beet', 'carrot'] }
Pritam Banerjee
  • 15,297
  • 10
  • 71
  • 92