0

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"
}

1 Answer 1

3

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";
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.