Javascript Lodash:过滤对象数组并检查是否为空
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36847925/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Lodash: Filter array of objects and check if not null
提问by kenshin9
I just started using lodash and have this array of objects, where one of the properties either has an integer or is null. I know how to filter the array for items that are null, but how do I check if it's not null?
我刚开始使用 lodash 并拥有这个对象数组,其中一个属性要么是整数,要么是空。我知道如何过滤数组中为空的项目,但如何检查它是否不为空?
Let's say I have something like this:
假设我有这样的事情:
var users = [
{ 'user': 'barney', 'default': 1 },
{ 'user': 'dino', 'default': 0 },
{ 'user': 'wilma', 'default': 1 },
{ 'user': 'fred', 'default': null }
];
And then I want something like:
然后我想要这样的东西:
var notNullDefault = _.filter(sourceData, ['is_default', ! null ]); // objects with names barney, dino, wilma
var nullDefault = _.filter(sourceData, ['is_default', null ]); // object with name fred
Again, I'm new to lodash, so maybe there's a better way to accomplish this too.
再说一次,我是 lodash 的新手,所以也许还有更好的方法来实现这一点。
Thanks in advance.
提前致谢。
回答by GG.
回答by Gruff Bunny
You could create a couple of predicate functions that check for default and not default users:
您可以创建几个谓词函数来检查默认用户而不是默认用户:
var isDefaultUser = function(user){
return _.isNull(user.default);
}
var isNotdefaultUser = _.negate(isDefaultUser);
They could then be used for filtering etc.
然后它们可以用于过滤等。
var defaultUser = _.filter(users, isDefaultUser);
var notDefaultUsers = _.filter(users, isNotdefaultUser);
// partition will be an array; first element contains the default users
// second element contains the non default users
var parition = _.partition(users, isDefaultUser);
回答by eildiz
let items = [1, 2, 4, null];
let isHaveNull = _.some(items, (item) => _.isNull(item));
console.log(isHaveNull);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.min.js"></script>
回答by Redu
I believe these libraries are sometimes just waste of time. Instead of learning a library dependent specific abstract it's best to get a good hold of ES6 array functions along with arrows. So here you go;
我相信这些库有时只是浪费时间。最好掌握 ES6 数组函数和箭头,而不是学习依赖于库的特定抽象。所以你来了;
var users = [
{ 'user': 'barney', 'default': 1 },
{ 'user': 'dino', 'default': 0 },
{ 'user': 'wilma', 'default': 1 },
{ 'user': 'fred', 'default': null }
],
userNull = [],
userFull = [];
users.forEach(e => e.default == void 0 ? userNull.push(e) : userFull.push(e));
document.write("<pre> userNull:\n" + JSON.stringify(userNull, null, 2) + "</pre>");
document.write("<pre> userFull:\n" + JSON.stringify(userFull, null, 2) + "</pre>");
回答by Tudor Constantin
Try with something like this:
尝试这样的事情:
var notNullDefault = _.filter(users, function(elem) { return elem.default !== null; });
var nullDefault = _.filter(users, function(elem) { return o.default === null; }); // object with name fred
The returned array contains only the elements for which the callback function returns true
.
返回的数组只包含回调函数返回的元素true
。
Or, as they put it in the fine manual: Iterates over elements of collection, returning an array of all elements predicate returns truthy for. The predicate is invoked with three arguments: (value, index|key, collection).
或者,正如他们在精美手册中所说的那样:迭代集合的元素,返回所有元素的数组谓词返回真值。谓词使用三个参数调用:(值、索引|键、集合)。