1

I have this array of objects [{x:0, y:1}, {x:3, y:2}] in javascript.

I want to to get an array of x only (0, 3) using the spread operator ... so I can apply javascript Math.max after.

2 Answers 2

5

You don't want to spread, though, you want to map (and then spread the result). So do that:

Math.max(...input.map(_=>_.x));
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, I try to assign this expression to a variable then find the max and min but it tells me it is a syntax error. var vector = ...input.map(=>.x) Math.max(vector) Math.min(vector)
0

You can use ES6 lambda function to achieve this. Please refer code below.

var input = [{"x":1,"y":2},{"x":3,"y":5},{"x":2,"y":2}];
console.log( Math.max( ...input.map(function(a){return a.x} ) ) );

1 Comment

How is this different than what is already posted? Please don't add unnecessary noise. And there is no lambda function in your code.

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.