0

I have a query object that looks like this

{
"regions": [],
"countries": [],
"channels": [],
"partners": [],
"branches": [],
"agents": []

}

After populating the arrays in the object it looks like this.

{
    "regions": [{
        "key": "101",
        "value": "Middle East(XX)"
    }],
    "countries": [],
    "channels": [],
    "partners": [{
        "key": "201",
        "value": "Partner A"
    }, {
        "key": "202",
        "value": "Partner B"
    }],
    "branches": [{
        "key": "401",
        "value": "Bangalore"
    }, {
        "key": "402",
        "value": "Chennai"
    }],
    "agents": [{
        "key": "501",
        "value": "IBM - Metlife"
    }]
}

I'm trying to loop through each of these arrays and determine if I should show the filter component. If any of the arrays in the object holds value, I should be showing the filter component

The code:

case false:
  let itemsInQuery = 0;
  Object.keys(query).forEach((item) => {
  itemsInQuery = query[item].length ? itemsInQuery++ : itemsInQuery;
  })
  itemsInQuery ? this.setState({showBubbles: true, query}) : this.setState({showBubbles: false, query})
  break;

I'm not sure what is wrong here, but itemsInQuery is always zero. Also, is there a better way to do this?

Thank you in advance!

3
  • what is the case true? Commented Jun 11, 2017 at 17:31
  • try this itemsInQuery += query[item].length ? 1:0 Commented Jun 11, 2017 at 17:33
  • true case code is out of context here and itemsInQuery variable exists only in the false case block Commented Jun 11, 2017 at 17:34

3 Answers 3

3

If any of the array have items in, you want a boolean to be true?

This sounds like a case for .some which on an array will take a predicate and return true if any item in that array matches the predicate.

const shouldShow = Object.keys(data).some(key => data[key].length > 0)

https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/some

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

4 Comments

Thank you! Learnt a new method on the array object today :) Cheers!!
I'm assuming that the loop will come to an end as soon as atleast one of the array is non-empty
Yes, that is how some works: it stops iterating as soon as one callback returns a truthy value. You can even leave out the > 0.
Yes correct, it terminates as soon as it finds a true value. Leaving the > 0 is personal preference, I find it a little unreadable without, but whatever suits your style most!
1

The problem is with this:

itemsInQuery = query[item].length ? itemsInQuery++

The ++ postfix operator returns the current value of itemsInQuery to the rest of the expression (i.e. 0) and then increments that variable to 1. But this is nullified by the assignment that happens afterwards to itemsInQuery which is the value 0.

So don't use ++ in an expression.

You can use this += instead:

itemsInQuery += query[item].length ? 1 : 0;

Comments

1

Does this do what you want?

var data1 = {
    "regions": [{
        "key": "101",
        "value": "Middle East(XX)"
    }],
    "countries": [],
    "channels": [],
    "partners": [{
        "key": "201",
        "value": "Partner A"
    }, {
        "key": "202",
        "value": "Partner B"
    }],
    "branches": [{
        "key": "401",
        "value": "Bangalore"
    }, {
        "key": "402",
        "value": "Chennai"
    }],
    "agents": [{
        "key": "501",
        "value": "IBM - Metlife"
    }]
};

var data2 = {
  "regions": [],
  "countries": [],
  "channels": [],
  "partners": [],
  "branches": [],
  "agents": []
}

function hasEntries (data) {
  for (var index in data) {
    if (Array.isArray(data[index]) && data[index].length) {
      return true;
    }
  }
  return false;
}

console.log(hasEntries(data1));
console.log(hasEntries(data2));

Comments

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.