1

I am trying to find a way to get an array of objects from a normal array without using a for loop.

An example for that I want to turn that array: [1,2,3,4,5]

To be an array of objects like that: [{x:1, y:2}, {x:2, y:4}, {x:3, y:6}, {x:4, y:8}, {x:5, y:10}] without using a for loop

Mureinik
316k54 gold badges403 silver badges406 bronze badges
asked Dec 30, 2022 at 16:45
2
  • 4
    What effort have you made? This seems to be a 'do my homework for me' question. Remember that SO is here to help you to debug code that you have written. It is not here to write code for you. Commented Dec 30, 2022 at 16:47
  • 2
    Kinda makes no sense as well, in the example you gave us you only have [1,2,3,4,5] but you want to "transform" that into an array of objects that have other elements that are not present in your initial array like 6,8,10 so what kind of rule are you even following to create your array? Commented Dec 30, 2022 at 17:48

1 Answer 1

4

You could map the array to a new array of objects:

result = [1,2,3,4,5].map(i => ({x: i, y: i*2}));
answered Dec 30, 2022 at 16:47
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.