0

Is there a better way in Javascript to check for multiple elements for their length. I want to check length of following arrays if they are greater than 0, I do:

a=[]; b=[1,2,3]; c=[db, gf, gf]; d=[]; e=[1,2,3]; f=[db, gf, gf]; g=[44,56,77]; 
h=[1,2,3]; i=[db, gf, gf];j=[]; e=[1,2,3]; k=[db, gf, gf]; l=[44,56,77]; 
m=[1,2,3]; n=[db, gf, gf]

So in total I have 14 arrays of diff values for diff condition and I want to apply just one solution to all of them if length ===0.

if (a.length === 0 || b.length === 0|| c.length === 0|| d.length < 0|| e.length === 0|| f.length === 0|| g.length === 0|| h.length === 0|| i.length === 0|| j.length === 0|| k.length === 0|| l.length === 0|| m.length === 0|| n.length === 0){
// set some field to false
}

is there a better way to check length, as this may get cumbersome if we have more arrays added Thanks!

3
  • 1
    why not use an array for the arrays? Commented Sep 19, 2018 at 19:58
  • 7
    an array length should never be less than 0... Commented Sep 19, 2018 at 19:59
  • sorry I meant ===, updated! Commented Sep 19, 2018 at 20:04

4 Answers 4

1

Put all the arrays in another array, then use Array#some

if ([a, b, c, d, e, f, g, j, i, j, k, l, m, n].some(x => x.length > 0)) {
    // do something
}

If you want to test if something is true for all the arrays, not just any of them, use every instead of some.

Sign up to request clarification or add additional context in comments.

Comments

1

If you put all your arrays in another array you can use array.every():

a=[]; b=[1,2,3]; c=['db', 'gf', 'gf']; d=[]; e=[1,2,3]; f=['db', 'gf', 'gf']; g=[44,56,77]; 
h=[1,2,3]; i=['db', 'gf', 'gf'];j=[]; e=[1,2,3]; k=['db', 'gf', 'gf']; l=[44,56,77]; 
m=[1,2,3]; n=['db', 'gf', 'gf']

let valid = [a, b, c, d, e, f, g, h, i, j, k, l, m, n].every(arr => arr.length > 0)
// nope some emtpies
console.log(valid)

// add something to the empty arrays
j.push(1)
d.push(2)
a.push("df")

valid = [a, b, c, d, e, f, g, h, i, j, k, l, m, n].every(arr => arr.length > 0)
// now valid
console.log(valid)

You might consider putting all this data in an array or object to start with; it will make things like this easier and will help organize your data.

Comments

1

every and some are your friends here. Just put your values into an array of their own and check in this case with some:

let a = [], b = [1,2,3], c = ['db', 'gf', 'gf'], d = [], e = [1,2,3], f = ['db', 'gf', 'gf'],
    g = [44,56,77], h = [1,2,3], i = ['db', 'gf', 'gf'], j = [], k = ['db', 'gf', 'gf'], 
    l = [44,56,77], m = [1,2,3], n = ['db', 'gf', 'gf'];
    
if ([a, b, c, d, e, f, g, h, i, j, k, l, m, n].some(arr => arr.length === 0)) {
  console.log('found empties');
} else {
  console.log('no empties');
}

if ([a, b, c, d, e, f, g, h, i, j, k, l, m, n].some(arr => arr.length > 5)) {
  console.log('found large');
} else {
  console.log('no large');
}

Comments

-1

Here is the code

for (var i = 65; i <= 78; i++) {
    if(window[String.fromCharCode(i)] < 0){
        // write code here
        // break;
    }
}

4 Comments

This will work only when variable are global variable not in functions
Furthermore, it only works it only work if the variables are named after the alphabet.
This approach looks very fragile. I would assume that in the actual problem, the values are not [1, 2, 3], etc. And quite possibly the names are not a, b, c, etc.
Yes It will work if variable name are alphabet, You can create array of simple variable like varaiableArray = ["var1", "var2"].. and then use this array instead of the string.fromcharecode

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.