3

Using underscore and I have an object array like so -

myObj = [{"car" : "red" },{"tree" : "green"}];

and I am being passed a new object that I need to find and overwrite an object with the same key, so I would be sent like

 {"car" : "blue" };

And I have to take the original object and change the car to blue. Is this possible with underscore? Thanks!

Edit - just to be clear, I am being given the {"car" : "blue"} and I need to compare it to the original object and find the "car" and replace it with the new value. Thanks!

ajmajmajma
  • 13,712
  • 24
  • 79
  • 133

2 Answers2

2

Sure. Assuming all of your objects only have one key:

var myArr = [ { "car" : "red" }, { "tree": "green" } ];

// First, find out the name of the key you're going to replace
var newObj = { "car": "blue" };
var newObjKey = Object.keys(newObj)[0]; // => "car"

// Next, get the index of the array item that has the key
var index = _.findIndex(myArr, function(obj) { return newObjKey in obj; });
// => 0

// Finally, replace the value
myArr[index][newObjKey] = newObj[newObjKey];

FYI findIndex is an Underscore.js 1.8 feature. If you're using an older version of Underscore, you'll need to replace that line with something like this:

var index;

_.find(myObj, function(obj, idx) {
  if("car" in obj) {
    index = idx;
    return true;
  }
});
Jordan Running
  • 102,619
  • 17
  • 182
  • 182
  • Thank you for the commenting, this is great. Just curious - what if the object has more than one key? Regardless, thank you, this answers my question. – ajmajmajma Mar 02 '15 at 21:28
  • 1
    If `newObj` has one more key, then you cannot predict which one of them `Object.keys(newObj)[0]` will return ([more information here](http://stackoverflow.com/questions/5525795/does-javascript-guarantee-object-property-order)). If the objects in `myArr` have more than one key, then the first one with a matching key will be updated. If you want to update all of the objects with matching keys, you could use [`where`](http://underscorejs.org/#where) (or just [`each`](http://underscorejs.org/#each)) instead. – Jordan Running Mar 02 '15 at 21:38
  • Sorry to bother but this line myArr[index][newObjKey] = newObj[newObjKey]; is giving me a syntax error of unidentified identifier, any clue on why? Thanks again! – ajmajmajma Mar 03 '15 at 17:10
  • 1
    I had forgotten the closing `)` on the previous line. ("Unexpected identifier" often means some earlier parentheses or brackets weren't closed correctly, or a comma is missing.) I've updated the code. – Jordan Running Mar 03 '15 at 18:54
0

another way you can do this would be as follows

myObj = [{"car" : "red" },{"tree" : "green"}];

let object2 = {"car": "blue"}

for(let obj of myObj){
  for(let key in obj){
    object2[key] ? obj[key] = object2[key] : ''
  }
}

That should dynamically replace anything from object2 which matches a key in an object within the array myObj

don't need any extra libraries or crazy variables :) Just good old fashioned javascript

EDIT Might not be a bad idea to include something like if(object2 && Object.keys(object2)){} around the for loop just to ensure that the object2 isn't empty/undefined

Jake Boomgaarden
  • 3,394
  • 1
  • 17
  • 31