JasonDavis JasonDavis - 2 years ago 135
Javascript Question

Get highest count number of array items from JavaScript object of arrays

I have a JavaScript object which is an object of arrays. When I print it to console it looks like this image below.

  • 3 Object keys 0, 1, and 2

  • Each object key has an array as its value

I need to get the highest number of array items from the object as a count number.

Right now each one has 3 array items but in my app there will be a different number for each one and I need to determine which has the highest number of items and save that number to a variable.\

How can I do this?

enter image description here

Answer Source

Here is another one:

function getHighest(obj){
  keys = Object.keys(obj);
    return 0;
  max = obj[keys[0]].length;
  for(i=1; i<keys.length; i++ )
      if(obj[keys[i]].length > max)
        max = obj[keys[i]].length;
  return max;

obj = 
  '0': ['a', 'b', 'c', 'd'],
  '1': ['a', 'b'],
  '2': ['a', 'b', 'c']


Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download