I have string
let str = '1,2,3,4,5,6,7,8,9,0';
I would like to make it as like this.
{
1:2,
3:4,
5:6,
7:8,
9:0
}
How can I do this ?
Only using map function can not solve this.
I have string
let str = '1,2,3,4,5,6,7,8,9,0';
I would like to make it as like this.
{
1:2,
3:4,
5:6,
7:8,
9:0
}
How can I do this ?
Only using map function can not solve this.
We can use Array.reduce() to do it
let str = '1,2,3,4,5,6,7,8,9,0';
let result = str.split(',').reduce((a,v,i,arr) => {
if(i%2 != 0){
a[arr.at(i-1)] = v
}
return a
},{})
console.log(result)