基本上,我有一个对象数组,我只想更新数组中满足条件的对象。我想知道是否有解决该问题的有效方法。现在我正在使用 lodash。这是一个例子:
var things = [
{id: 1, type: "a", value: "100"},
{id: 2, type: "b", value: "300"},
{id: 3, type: "a", value: "100"}
];
var results = _.map(things, function (thing) {
if(thing.type === "a") {
thing.value = "500";
}
return thing;
});
// => results should be [{id: 1, type: "a", value: "500"}, {id: 2, type: "b", value: "300"}, {id: 3, type: "a", value: "500"}];
最佳答案
这里不需要使用map
方法。
您可以通过向其传递一个回调函数来使用一个简单的forEach
函数。
var results = _.forEach(things, function (thing) {
if(thing.type === "a") {
thing.value = "500";
}
});
关于javascript - 是否有 lodash 函数或 'lodash way' 来执行条件 _.map?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47231924/