'How to create an array of object with 2 arrays .js [duplicate]
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
Solution 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)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
