Thursday, May 23, 2024
 Popular · Latest · Hot · Upcoming
153
rated 0 times [  160] [ 7]  / answers: 1 / hits: 16483  / 7 Years ago, tue, october 31, 2017, 12:00:00

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.


More From » json

 Answers
3

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
}
});

[#56055] Friday, October 27, 2017, 7 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
braeden

Total Points: 231
Total Questions: 96
Total Answers: 86

Location: Somalia
Member since Mon, Dec 28, 2020
3 Years ago
braeden questions
Fri, Oct 29, 21, 00:00, 3 Years ago
Thu, Oct 10, 19, 00:00, 5 Years ago
;