gpt4 book ai didi

javascript - 如何制作 `where not` 大小写?

转载 作者:数据小太阳 更新时间:2023-10-29 04:03:04 31 4
gpt4 key购买 nike

我需要 where 但 not 大小写。例如,我想找到没有名字“莎士比亚”的戏剧:

_.where(listOfPlays, {author: !"Shakespeare", year: 1611});
^^^^^^^^^^^^^
NOT Shakespeare

如何使用 underscore 来实现?

最佳答案

_.filter(listOfPlays, function(play) {
return play.author !== 'Shakespeare' && play.year === 1611;
});

http://underscorejs.org/#filter

where 只不过是 filter 的一个方便的包装器:

// Convenience version of a common use case of `filter`: selecting only objects
// containing specific `key:value` pairs.
_.where = function(obj, attrs) {
return _.filter(obj, _.matches(attrs));
};

https://github.com/jashkenas/underscore/blob/a6c404170d37aae4f499efb185d610e098d92e47/underscore.js#L249

关于javascript - 如何制作 `where not` 大小写?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26143852/

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