0

I'm trying to get the object with the greatest number of elements and get the number of elements.

I currently have the following:

var array = [
  [{
    'id': 1,
    'value': 100
  }, {
    'id': 1,
    'value': 100
  }],
  [{
    'id': 1,
    'value': 100
  }, {
    'id': 1,
    'value': 100
  }, {
    'id': 1,
    'value': 100
  }],
  [{
    'id': 1,
    'value': 100
  }, {
    'id': 1,
    'value': 100
  }]
];

for (var i = 0; i < array.length; i++) {
  console.log(array[i].length);

  //Here I need to get the bigger number in this case is 3, because I need it to another validations inside this for loop
}

In this case I know what is the bigger object, but I need to get the number and save it in a variable because I need to do some validations after.

I hope that you can understand me and help me

Fabian Sierra
  • 766
  • 1
  • 8
  • 22
  • You could just create an array of the lengths via [How to count array elements inside an array in javascript?](https://stackoverflow.com/q/47885719/215552), then get the max via [Find the min/max element of an Array in JavaScript](https://stackoverflow.com/q/1669190/215552) – Heretic Monkey Nov 13 '18 at 17:38

4 Answers4

1

You can use reduce to get the largest subarray (and then its length):

var array = [
  [{
      'id': 1,
      'value': 100
    },
    {
      'id': 1,
      'value': 100
    }
  ],
  [{
      'id': 1,
      'value': 100
    },
    {
      'id': 1,
      'value': 100
    },
    {
      'id': 1,
      'value': 100
    }
  ],
  [{
      'id': 1,
      'value': 100
    },
    {
      'id': 1,
      'value': 100
    }
  ]
];

var largest = array.reduce((a, c) => a.length < c.length ? c : a);
console.log(largest);
console.log(largest.length);
slider
  • 12,810
  • 1
  • 26
  • 42
0

You can simply sort by length in descending order and get the top element:

var array = [ [{ 'id': 1, 'value': 100 }, { 'id': 1, 'value': 100 }], [{ 'id': 1, 'value': 100 }, { 'id': 1, 'value': 100 }, { 'id': 1, 'value': 100 }], [{ 'id': 1, 'value': 100 }, { 'id': 1, 'value': 100 }] ];

const r = array.sort((a,b) => b.length - a.length)[0]

console.log('element:', r, ', length:', r.length)
Akrion
  • 18,117
  • 1
  • 34
  • 54
0

The follwoing code should find the longest array and display its content and length. Is there some other data you need to find and store in a variable?

var array = [
  [{
    'id': 1,
    'value': 100
  }, {
    'id': 1,
    'value': 100
  }],
  [{
    'id': 1,
    'value': 100
  }, {
    'id': 1,
    'value': 100
  }, {
    'id': 1,
    'value': 100
  }],
  [{
    'id': 1,
    'value': 100
  }, {
    'id': 1,
    'value': 100
  }]
];

var result = {
    length: 0
};

for (var i = 0; i < array.length; i++) {
  if(array[i].length >= result.length){
      result = array[i];
  } 
}

console.log(result.length);
console.log(result);
Tornike Shavishvili
  • 1,244
  • 4
  • 16
  • 35
0

Iterate through each element of array and check length of each sub array and store index of max length of subarray.

var array = [ [{ 'id': 1, 'value': 100 }, { 'id': 1, 'value': 100 }], [{ 'id': 1, 'value': 100 }, { 'id': 1, 'value': 100 }, { 'id': 1, 'value': 100 }], [{ 'id': 1, 'value': 100 }, { 'id': 1, 'value': 100 }] ];

var subArrayLength = 0;
var index = 0; 
for(var i = 0; i < array.length; i++)
{
   if(subArrayLength < array[i].length){
            index = i;
            subArrayLength = array[i].length;
   }
}

console.log(array[index]);
console.log(index);
Prasad Telkikar
  • 15,207
  • 5
  • 21
  • 44