gpt4 book ai didi

javascript - 如何使用 lodash _.map 删除 key

转载 作者:行者123 更新时间:2023-11-28 11:32:51 29 4
gpt4 key购买 nike

我有一个 lodash 映射函数,有时在给定键的重要属性中具有空值,在这种情况下,我想从结果中完全删除该键。我该怎么做?

仅尝试了 if (_.isEmpty(key.thing)) { delete key } 但这不起作用 - 它实际上破坏了应用程序。

最佳答案

您可以使用reduce函数并过滤其中的空值。

_.reduce(yourArray, function(result, currentItem) {
var itemAfterSomeOperations;

if (!_.isEmpty(currentItem.thing)) {
//here you can do any operations like in your _.map handler function
//and then push the updated item after your operations in the resulted array

itemAfterSomeOperations = someOperationOnItemAndReturnNewValue(currentItem);

result.push(itemAfterSomeOperations);
}

return result;
}, []);

您不需要删除键,因为 _.map 以及 _.reduce 将返回包含您想要的任何项目的新数组。请记住,mapfilterfind 等可以通过 reduce 实现。

关于javascript - 如何使用 lodash _.map 删除 key ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34980737/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com