-1

I have an array of objects:-

var arr1 = [{
    "name": "A"
}, {
    "name": "B"
}, {
    "name": "C"
}, {
    "name": "D"
}];

I want to add a new property to each element which are not present in the second array.

var arr2 = [C,D]

The resulted array should be:-

arr1 = [{
    "name": "A",
    "country":"USA"
}, {
    "name": "B",
    "country":"USA"
}, {
    "name": "C"
}, {
    "name": "D"
}];

Can any one tell me how to achieve that?

sg055322
  • 161
  • 2
  • 15
  • My requirements were different. I have changed the previous question. Please help with the solution. – sg055322 Sep 16 '17 at 09:27

2 Answers2

6

I want to add a new property to each element which are not present in the second array.

That completely changes the question you originally asked.

If the items are mutable, you'd just loop through the array adding the property to selected items. There are many ways to do that, such as forEach:

arr1.forEach(function(item) {
    if (arr2.indexOf(item.name) === -1) {
        item.country = "USA";
    }
});

Live Example:

var arr1 = [{
    "name": "A"
}, {
    "name": "B"
}, {
    "name": "C"
}, {
    "name": "D"
}];

var arr2 = ["C", "D"];

arr1.forEach(function(item) {
    if (arr2.indexOf(item.name) === -1) {
        item.country = "USA";
    }
});
console.log(arr1);

If they're immutable, you'd use map instead:

arr1 = arr1.map(function(item) {
    if (arr2.indexOf(item.name) === -1) {
        return /*...create item with `country = "USA"` here...*/;
    } else {
        return item;
    }
});

Live Example:

var arr1 = [{
    "name": "A"
}, {
    "name": "B"
}, {
    "name": "C"
}, {
    "name": "D"
}];

var arr2 = ["C", "D"];

arr1 = arr1.map(function(item) {
    if (arr2.indexOf(item.name) === -1) {
        return {name: item.name, country: "USA"};
    } else {
        return item;
    }
});
console.log(arr1);

Original answer: (to your original question)

You would loop over the resulting array adding the property. There are many ways to do that, such as forEach:

arr1.forEach(function(item) {
    item.country = "USA";
});

Technically, you could make it a side-effect of your filter, like this:

var arr1 = arr1.filter(function(item){
  if (arr2.indexOf(item.name) === -1) {
    item.country = "USA";
    return true;
  };
  return false;
});

BUT, side-effects in filter callbacks (and similar) are generally not a great idea. Unless you know that the resulting array (arr1) is hundreds of thousands of entries, the second pass keeps things simple by filtering and then modifying.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • The result as shown should have objects with name C and D, just not add the country. With filter those items would be dropped right? `map` sounds like a better option here? – sabithpocker Sep 16 '17 at 09:35
  • 1
    @sabithpocker: The OP completely changed the question, so I'm re-answering it. But no, `map` wouldn't be the solution here (unless the items are immutable). – T.J. Crowder Sep 16 '17 at 09:38
0

Use Array.forEach and add the property if the element is not present - by using the same comparison you were already using.