If i have the following array
const Array = ['Michael', 'student', 'John', 'cop', 'Julia', 'actress']
How could i make an object like
const Object = {
Michael: student,
John: cop,
Julia: actress,
}
Is there a way to make the even index elements to be the object’s keys and the odd index elements to be the key’s values?
>Solution :
A simple for loop that steps by two indexes at a time would work.
Try like below
const array = ['Michael', 'student', 'John', 'cop', 'Julia', 'actress'];
const output = {}
for(let i = 0; i < array.length; i+=2){
output[array[i]] = array[i+1]
}
console.log(output);