I have one array with key and its array elements
When I am converting it from array to object then I am getting index 0 and 1 but here I need index should be array1 and array2
let input = [{
"array1": [{
"id": 1,
"name": "Test 1"
}, {
"id": 2,
"name": "Test 2"
}]
},
{
"array2": [{
"id": 1,
"name": "Test 1"
},
{
"id": 2,
"name": "Test 2"
}
]
}
];
function convert(arr) {
let obj = new Object();
arr.forEach((value, key) => {
obj = value;
});
return obj;
}
console.log(convert(input))
so after calling this function i am getting following output
{
"array2": [
{
"id": 1,
"name": "Test 1"
},
{
"id": 2,
"name": "Test 2"
}
]
}
but here I need output should be like this
{
"array1": [
{
"id": 1,
"name": "Test 1"
},
{
"id": 2,
"name": "Test 2"
}
],
"array2": [
{
"id": 1,
"name": "Test 1"
},
{
"id": 2,
"name": "Test 2"
}
]
}
If I use the define the array inside the convert function and push it then again i am getting the index 0 and 1.
How can i get expected result here.