I want to convert the below JSON format
let init = {
test1: [1, 2, 3, 4, 5],
test2: [6, 7, 8, 9, 10]
}
into this format using javascript native methods of array or object (supported in all browsers)
[{
test1: 1,
test2: 6
},{
test1: 2,
test2: 7
},{
test1: 3,
test2: 8
},{
test1: 4,
test2: 9
},{
test1: 5,
test2: 10
}]
I have tried with for loop and below is the code,
let keys = Object.keys(init);
let result = [];
for(let i = 0; i < keys.length; i++){
let key = keys[i];
for(let j = 0; j < init[key].length; j++){
if(i === 0){
let obj1 = {};
obj1[key] = init[key][j];
result.push(obj1);
}else{
let obj2 = result[j];
obj2[key] = init[key][j];
}
}
}