0

I have an array of arrays and I would like to find the max value of the first elements in those arrays.

Example:

[ [ 300, 600 ], [ 160, 600 ], [ 120, 600 ] ]

This example should return 300 because it is the max value of 300, 160 and 120.

This is an easy task and can be solved like this:

let first = array.map(item => item[0]);
let max = Math.max(...first); // max == 300

The problem is that I work with TypeScript and this array potentially can have a fixed string in it like so:

[ 'something' ]

No matter what I try to do I get errors from different places. For example I try something like this:

array.filter(item => item != "something").map(item => item[0]);

I cannot change the original array. So how can I overcome this issue with TypeScript? Thanks for help.

4 Answers 4

2

Filter out only number values using isNaN after getting the list of first values from array inside the array

let first = array.map(item => item[0]).filter(val => !isNaN(val));

Then perform Math.max on the resulted array

let max = Math.max(...first); 
Sign up to request clarification or add additional context in comments.

3 Comments

TS2345: Argument of type 'ReactText' is not assignable to parameter of type 'number'. Type 'string' is not assignable to type 'number'.
Is this error because of adding the code in answer to your app? Or is it the error raised because of subsequent lines/elsewhere in code?
Error might be because of changing the name of variable. Updating my answer
0

The point is that filter and map methods are immutable and do not change the original array.
There should be no problem with this snippet:

const max = Math.max(...(array
  .filter(item => item !== "sth")
  .map(item => item[0])
));

2 Comments

TS2345: Argument of type 'ReactText' is not assignable to parameter of type 'number'. Type 'string' is not assignable to type 'number'.
You can assign filtered array a tuple type of (number, number). it ensures typescript that all items are arrays with your map requirements.
0

You can change map function to map strings to integer min so that they do not affect your max calculations

let first = array.map(item => typeof(item[0]) !== "string" ? item[0] : Number.MIN_VALUE);
let max = Math.max(...first); // max == 300

Comments

0

Try to filter array like this:

const array = [[300, 600], [160, 600], [120, 600], ["something"]];
let first = array
  .filter(item => !isNaN(Number(item[0])))
  .map(item => Number(item[0]));
let max = Math.max(...first);

console.log('Max', max);

When item[0] is not a number Number() returns NaN that is filtered out by checking !isNaN(Number(item[0])).

2 Comments

I like this one but when it deals with the string it returns "-Infinity"
Could you post an example of array you filtering?

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.