get the maximum array length of of map array values in javascript in javascript

To get the maximum array length of map array values in JavaScript, you need to loop through the map and get the size of each array value. Then, you can compare the sizes and get the maximum length. Here's a code snippet that demonstrates this:

index.tsx
// create a sample map
const map = new Map([
  ["key1", [1, 2, 3]],
  ["key2", [4, 5]],
  ["key3", [6, 7, 8, 9]]
]);

let maxLength = 0;
for (let value of map.values()) { // iterate through the map values
  maxLength = Math.max(maxLength, value.length); // get the maximum length
}

console.log(maxLength); // output: 4
320 chars
14 lines

In this example, we create a sample map with array values of different lengths. We use a for...of loop to iterate through the map values and get the length of each array value using the length property. Then, we use the Math.max() method to get the maximum length of all the array values. Finally, we output the result to the console using console.log(). The output in this case is 4, since the longest array in the map has a length of 4.

related categories

gistlibby LogSnag