I have an array of objects and I want to update some of the content. I figured I could just map through the objects, find the match I was looking for and than update it.
data = data.map(obj => {
return this.state.objToFind === obj.title;
}).map(obj, idx) => {
console.log("found " + obj.title); // reads found + undefined?
obj.menu = this.state.menu;
obj.title = this.state.title;
obj.content = this.state.content;
});
However, this is not working. I find the object but obj.anything is undefined. My console.log reads "Found undefined".
EVEN SIMPLER
You could use the some operator. (It works by iterating over the array, when you return true it breaks out of the loop)
data.some(function(obj){
if (obj.title ==== 'some value'){
//change the value here
obj.menu = 'new menu';
obj.title = 'new title';
return true; //breaks out of he loop
}
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With