Suppose that I have an json as follow.How could i replace the json status to inactive based on the key status.
Keyword: replace, underscore
{
"id": "0001",
"name": "Test",
"status": "active"
}
You can use _.each() for this
_.each(YourArray, function(p){
if(p.status === "active") p.status = "inactive";
// or
p.status = p.status === "active" ? "inactive" : p.status;
});
But why You want use underscore for this task? You can resolve it with "vanilla" js:
for(var i = 0, l = YourArray.length; i < l; i++){
if(YourArray[i].status === "active") YourArray[i].status = "inactive";
}
Or use Array.prototype.forEach()
YourArray.forEach(function(p){
if(p.status === "active") p.status = "inactive";
});