1

I have 3 arrays:

keys = ['first','second']
ycor = [200,400]
xcor = [[375,75],[75]]

I am thinking of how to get them to:

[
  {
    'first': 200,
    'second': [375,75]
  },
  {
    'first': 400,
    'second': [75]
  }
]

The primary attempt was to use the forEach() function, but only managed to reach a 2 sided case and unidentifiable by node.

var result = []
keys.forEach((i,v,w) => result[i] = (xcor[v],ycor[w]))

Is it possible at all?

1
  • just made an update, thank you Commented Jul 3, 2020 at 16:31

1 Answer 1

2

You could reduce the values arrays and map new objects.

This approach uses spread syntax ... for taking the old object at the same index j of the result array.

var keys = ['first', 'second'],
    ycor = [200, 400],
    xcor = [[375, 75], [75]],
    result = [ycor, xcor].reduce(
        (r, a, i) => a.map((v, j) => ({ ...r[j], [keys[i]]: v })),
        []
    );

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

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.