let arr = ['drink', 'soda', 'name', 'john', 'someKey', 'someValue']
I’d like to assign key value pairs of the strings i have in the array above
for example:
[{drink: 'soda'},
{name: 'john'},
{someKey:'someValue'}
]
I’ve spent hours trying to figure this out… I’ve tried approaching it with the map method but my limited knowledge on javascript is consistently returning errors…
I’d really appreciate it if someone could help me out. Thanks
>Solution :
You can do it with a simple for loop:
let arr = ['drink', 'soda', 'name', 'john', 'someKey', 'someValue'];
let result = [];
for(let i = 0; i < arr.length; i+=2) {
let obj = {}
obj[arr[i]] = arr[i+1];
result.push(obj)
}
console.log(result)