Need help converting to object
[1, 2, [4, 5, 6], 7, 8, [9, 0], 11, 12]
Desired output:
{
1: 1,
2: 2,
'array1': {
4: 4,
5: 5,
6: 6
},
7: 7,
8: 8,
'array2': {
9: 9,
0: 0
},
11: 11,
12: 12
}
Actual output received is: { 1: 1, 11: 11, 12: 12, 2: 2, 7: 7, 8: 8 }
This is my code:
input = [1,2,[4,5,6],7,8,[9,0],11,12];
obj = {}
const convert = (arr) =>{
for (i = 0; i < arr.length; i++){
if(!Array.isArray(arr[i])){
obj[arr[i]] = arr[i];
}
}}
convert(input);
console.log(obj);