0

I have the following array of objects and I want to find the highest weight of any person in any year (the weight is enough, I don't need the corresponding year or name).

Thanks in advance :)

var arr = [
    {
        'name': 'Bob',
        'weights': [
            {
                'weight': 90,
                'year': 2018
            },
            {
                'weight': 85,
                'year': 2017
            },
            // etc.
        ]
    },
        'name': 'Charlie',
        'weights': [
            {
                'weight': 65,
                'year': 2018
            },
            {
                'weight': 60,
                'year': 2017
            },
            // etc.
        ]
    },
    // etc.
]

1 Answer 1

3

You could use Math.max and spread syntax to find max value but you also need to use map to get weight values in one array first.

var arr = [{"name":"Bob","weights":[{"weight":90,"year":2018},{"weight":85,"year":2017}]},{"name":"Charlie","weights":[{"weight":65,"year":2018},{"weight":60,"year":2017}]}]

var max = Math.max(...[].concat(...arr.map(({weights}) => weights.map(({weight}) => weight))))
console.log(max)

Sign up to request clarification or add additional context in comments.

Comments

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.