how to change array into object in javascript code example

Example 1: javascript convert array to object

function arrayToObject(arr) {
    var obj = {};
    for (var i = 0; i < arr.length; ++i){
        obj[i] = arr[i];
    }
    return obj;
}
var colors=["red","blue","green"];
var colorsObj=arrayToObject(colors);//{0: "red", 1: "blue", 2: "green"}

Example 2: how to convert object to array in javascript

const propertyValues = Object.values(person);

console.log(propertyValues);

Example 3: js array to object with keys

const arr = ['a','b','c'];
const res = arr.reduce((a,b)=> (a[b]='',a),{});
console.log(res)