I have a function that takes in a JSON object and edits a value based on a condition. I am struggling to find the correct way to return the new updated JSON object. When I console.log(testBookmarks) after I see the updated object but when I stringify it and try to use it in another function it seems to be the old non-updated object.
Function:
const iterate = (obj) => {
Object.keys(obj).forEach(key => {
// console.log('key: '+ key + ', value: '+obj[key]);
if(key === "url"){
fetch(obj[key])
.then(response => {
console.log(response)
console.log(response.status);
if(response.status != 200){
console.log(obj)
obj[key] = "null"
console.log(obj)
return
}
})
.catch(error => console.error(error));
}
if (typeof obj[key] === 'object') {
iterate(obj[key])
}
})
}
iterate(testBookmarks)