Rasim Avcı
Rasim Avcı

Reputation: 1213

How to check the array length of multiple arrays in an object?

I have a functions which accepts browser types as arrays in an object as argument. I want to return an error message for whether user wanted any browser or not. For this I used a variable named allTypeNumber. I used for returning error in the code below.

I want to check the length of every array and if they're all 0, I know that no browser has been requested, but confused how to do that without using a variable.

async retrievePartners (capabilities) {
    const appropriatePartners = { chrome: [], firefox: [], safari: [], ie: [] }
    const partners = await this.getAllPartners()
    let allTypeNumber = 0
    // first check if there is available appropriate Partners
    Object.keys(capabilities.type).forEach(key => {
      let typeNumber = parseInt(capabilities.type[key])
      allTypeNumber = allTypeNumber + typeNumber
      for (let i = 0; i < typeNumber; i++) {
        partners.forEach((partner, i) => {
          if (
            key === partner.value.type &&
            partner.value.isAvailable &&
            appropriatePartners[key].length < typeNumber
          ) {
            appropriatePartners[key].push(partner)
          }
        })

        if (appropriatePartners[key].length < typeNumber) {
          throw new Error(
            'Sorry there are no appropriate Partners for this session'
          )
        }
      }
    })

    if (allTypeNumber === 0) {
      throw new Error('Please mention at least 1 type of browser !')

and I call it like with this parameter

const capabilities = { type: { chrome: 1 } }

Upvotes: 1

Views: 2157

Answers (1)

samanime
samanime

Reputation: 26547

A clean way to check if every value of an array is something, is to use every() (or its inverse, some()). every() will loop through each element in an array and continue as long as it keeps getting a truthy value returned. some() will keep going as long as it keeps getting a falsey. They both return either true or false based on their last return value.

Or, if you need a count (versus just knowing if there is at least one) you can use reduce() to create a sum. reduce() loops through an array with an accumulator and is good for creating singular values from an array of data.

Also, if you need to get the values from an object, using either Object.entries() or Object.values() can be cleaner than using keys. Object.entries() gives you an array of arrays, where each array is [key, value]. Object.values() just gives you an array of values.

Finally, for making sure the partner is appropriate, you can use partner.filter() to filter only the good partners and the bad partners.

Putting some of those together, you can easily do something like:

const total = Object.entries(capabilties.type).reduce(([key, type]) => {
  typeNumber = parseInt(type);
  const good = partners.filter(partner => key === partner.value.type && partner.value.isAvailable && appropriatePartners[key] < typeNumber);

  appropriatePartners[key].concat(good);

  if (appropriatePartners[key].length < typeNumber) {
    throw new Error('No appropriate partners');
  }

  return typeNumber;
}, 0);

if (total === 0) {
  throw new Error('Please mention at least 1 type of browser');
}

Of course, this isn't the only appropriate. I don't know which bits of your logic are simply so you can keep track and which have business logic behind them. Using some of the functions I mentioned, you can probably reduce it even further if you don't need to keep track of certain things.

Upvotes: 2

Related Questions