最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to check if array contains more than one element? - Stack Overflow

programmeradmin1浏览0评论

I would like to ensure that an array contains more then one element and that it is not null nor undefined. With other words: I am looking for an alternative to

if(array && array.length > 1)

that would be better to read.

Lodash provides a method for checking if an array contains at least one element:

if(_.some(array))

Is there a similar method for checking for several elements?

Unfortunately following methods do not exist:

if(_.several(array))

if(_.many(array))

Would I have to write my own extension method or did I just not find the right lodash mand, yet?

I would like to ensure that an array contains more then one element and that it is not null nor undefined. With other words: I am looking for an alternative to

if(array && array.length > 1)

that would be better to read.

Lodash provides a method for checking if an array contains at least one element:

if(_.some(array))

Is there a similar method for checking for several elements?

Unfortunately following methods do not exist:

if(_.several(array))

if(_.many(array))

Would I have to write my own extension method or did I just not find the right lodash mand, yet?

Share Improve this question edited May 18, 2021 at 19:10 Alexander Abakumov 14.6k16 gold badges98 silver badges133 bronze badges asked Jul 7, 2017 at 9:35 StefanStefan 12.4k9 gold badges77 silver badges137 bronze badges 3
  • 3 for me array.length > 1 is a lot more readable then _.some(array) – SD. Commented Jul 7, 2017 at 9:37
  • 1 Looks like you are overthinking here. _.some is not meant for the purpose you mentioned. It's much more powerful. For you purpose writing a function which wraps array.length > 1 more than suffices. – Mukesh Soni Commented Jul 7, 2017 at 9:38
  • @Stoyan _.some(array) would pare to array && array.length > 0. I am reading from left to right and when getting to "some" and see the argument I already know what the condition is about. Otherwise I need to proceed to "length" and even then I have to check what es after length: <, > ===, ... – Stefan Commented Jul 7, 2017 at 9:43
Add a ment  | 

3 Answers 3

Reset to default 2
_.pact(array).length > 1

Compact returns array with falsey values removed (false, null, 0, "", undefined, and NaN).

This is the atLeast() function, which works like _.some(), but accepts a min number. If the callback returns true a number of times that is identical to min, the function will return true.

 // the parameters order is to maintain patability with lodash. A more functional order would be - cb, min, arr
function atLeast(arr, min, cb) {
  cb = cb || _.identity; // default is like some
  
  var counter = 0;
  return _.some(arr, function(v) {
    cb(v) && counter++;
    
    return counter === min;
  });
}

_.mixin({ 'atLeast': atLeast });

var arr1 = [1, 0, null, undefined];
var arr2 = [null, undefined, 0];

var isNotNil = _.negate(_.isNil); // not null or undefined

console.log(_.atLeast(arr1, 2, isNotNil));

console.log(_.atLeast(arr2, 2, isNotNil));
<script src="https://cdnjs.cloudflare./ajax/libs/lodash.js/4.17.4/lodash.min.js"></script>

You can create own lodash mixin for that:

var myArray = [1, 2, 3];
var anotherArray = [4];

function several(array) {
  return _.get(array, 'length') > 1
}

_.mixin({ 'several': several });

console.log(_.several(myArray)) // ==> true
console.log(_.several(anotherArray)) // ==> false

Example: https://jsfiddle/ecq7ddey/

发布评论

评论列表(0)

  1. 暂无评论