1

Is there some native/lodash/underscore/etc method to check if an obj is an empty array? [] === [] returns false due to different obj references. I wrote a quick checker for it:

  function isArrayOfLength(obj, length) {
    var isArrayOfSpecifiedLength = false; 

    if(Array.isArray(obj)){ 
      if(obj.length === length){
        isArrayOfSpecifiedLength = true; 
      }
    }

    return isArrayOfSpecifiedLength; 
  } 

but I don't want to clutter up production code if something better is available. Plunk if you want it for whatever reason. Note - I need to be able to check any var type - the method might get an obj or an int, so I can't just check length without verifying that it's an array.

VSO
  • 11,546
  • 25
  • 99
  • 187
  • http://underscorejs.org/#isEmpty – djaszczurowski Nov 02 '16 at 14:15
  • @djaszczurowski Yea, I saw that, but it looks like it will pass for an object as well... "Returns true if an enumerable object contains no values (no enumerable own-properties)." – VSO Nov 02 '16 at 14:16
  • 1
    Possible duplicate of [Is object empty?](http://stackoverflow.com/questions/4994201/is-object-empty) – Huangism Nov 02 '16 at 14:17
  • @canon: I am focused on anything that distinguishes between an empty array and anything else, without throwing. – VSO Nov 02 '16 at 14:17
  • 3
    `Array.isArray(myArray) && myArray.length === 0` – Rajesh Nov 02 '16 at 14:17
  • (function(){ var obj = {lol: 'test'}; var isArray = obj.length; console.log(isArray); })(); returns undefined Raj. – VSO Nov 02 '16 at 14:19
  • @Rajesh Though I guess undefined is falsy, and it's fine since it doesnt throw, so that might be the best solution. – VSO Nov 02 '16 at 14:20
  • @canon doesn't the answer contain everything that is asked? – Huangism Nov 02 '16 at 14:21
  • @VSO is the expected parameter an `array` or `array || object` – Rajesh Nov 02 '16 at 14:21
  • @Rajesh array || object – VSO Nov 02 '16 at 14:22
  • @canon: The latter. – VSO Nov 02 '16 at 15:34
  • @canon thanks for the explaining. I have delete my answer and have marked it as duplicate. Just a pointer tough, **Please comment** if you downvote. This will ensure your message is conveyed. I missed you conversation and was unaware about duplicate. Still thanks – Rajesh Nov 03 '16 at 06:30

3 Answers3

1

It's super-simple:

function isEmptyArray(obj) {
   return Array.isArray(obj) && obj.length === 0;
}
Ginden
  • 5,149
  • 34
  • 68
-1

Try this

var ap = (Array.isArray(obj) && (obj.length === 0))? true : false;

if ap evaluates to true then you will know its an empty array

Edwin Kato
  • 543
  • 3
  • 11
  • 1
    Both `Array.isArray(obj)` and `(obj.length === 0)` will return a boolean value. You **do not** need a ternary operator. – Rajesh Nov 02 '16 at 14:33
  • Yes both will return a boolean and the 2 booleans will then be evaluated eg true && false = true. true && true will evaluate to true thus giving us what we wanted to know in the first place – Edwin Kato Nov 02 '16 at 14:37
  • Yes both will return a boolean and the 2 booleans will then be evaluated eg true && false = true. true && true will evaluate to true thus giving us what we wanted to know in the first place. The ternary operator is more precise – Edwin Kato Nov 02 '16 at 14:38
  • 1
    What your expression will look like `return true? true : false` or `return false? true : false`. So instead of again evaluating, you can return output of expression. – Rajesh Nov 02 '16 at 14:43
  • No @Rajesh. Javascript will evaluate the entire expression in one go and the whole thing will be solved in just one line of code. Try it out in your console. I just did – Edwin Kato Nov 02 '16 at 14:49
  • @EdwinKato the ternary operator is totally unnecessary in this case. You could simply write `var ap = Array.isArray(obj) && obj.length === 0;`. The entire right side of that assignment is already a boolean expression. Sure, your version functions, but it's doing extra work and it's more verbose than it needs to be. Raj is right. I'm not sure what you're arguing here. – canon Nov 02 '16 at 18:18
  • 1
    Oooh i see. Your explanation is clearer. Thanx @Canon and i get it now – Edwin Kato Nov 03 '16 at 06:12
-2

With Ramda you can use R.isArrayLike([]); //=> true to check if it's an array, and R.isEmpty([]); //=> true to verify if has data.

Hosar
  • 5,163
  • 3
  • 26
  • 39