Q
Q
Quambaras2021-06-19 11:44:17
JavaScript
Quambaras, 2021-06-19 11:44:17

How to find an object in an array with arrays whose value is greater than other objects?

const arr = [
   [
      {name: 'Name1', age: 3},
      {name: 'Name1', age: 5}
   ],
    [
      {name: 'Name1', age: 1},
      {name: 'Name1', age: 7}
   ]
   [
     {name: 'Name1', age: 2},
     {name: 'Name1', age: 10}
   ]
]


how to find the largest value in the age field from such an array?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
0
0xD34F, 2021-06-19
@Quambaras

In the title of the question:

find object

In the text:
find the largest value

Well, what the hell? What do you need, right? Do you even know yourself?
First:
arr.flat().reduce((max, n) => max?.age > n.age ? max : n, null)

Second:
Math.max(...arr.flat().map(n => n.age))

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question