假设我有一个如下的 json。我如何根据键 status
将 json status
替换为 inactive
。
关键字:替换、下划线
{
"id": "0001",
"name": "Test",
"status": "active"
}
最佳答案
您可以使用_.each()
为此
_.each(YourArray, function(p){
if(p.status === "active") p.status = "inactive";
// or
p.status = p.status === "active" ? "inactive" : p.status;
});
但是为什么你想使用 underscore为了这个任务?您可以使用“vanilla”js 解决它:
for(var i = 0, l = YourArray.length; i < l; i++){
if(YourArray[i].status === "active") YourArray[i].status = "inactive";
}
YourArray.forEach(function(p){
if(p.status === "active") p.status = "inactive";
});
关于javascript - 使用下划线查找并替换对象属性值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37417442/