-3

I have two arrays

study_id: ["1", "2", "3"],
study_name: ["clinic Study", "study test", "test 2"],

I want to create an array of objects with the array as follows:

  "studies": [{
      "study_id": "1",
      "study_name": "clinic study"
    },
    {
      "study_id": "2",
      "study_name": "test study"
    }
  ]

How to achieve this? thanks

1
  • you need to be sure that the order of both arrays if correct though, it's prone to errors Commented May 23, 2022 at 6:49

1 Answer 1

-1

there are a few ways to do this but one ES6 solution is to use map method in first array and map each element to a object which key is element from first array and value is element with same index in the second array.

var a = [1, 2, 3]
var b = ['a', 'b', 'c']

var c = a.map((e, i) => {
  return { [e]: b[i] };
});

console.log(c)

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

1 Comment

There is no ES6 specific feature in the code.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.