Search code examples
javascriptarraysobjectconverters

I want to convert array to object in java script like[ ["key1","ans1"],["key2","ans2"] ]=> {key1:"ans1",key2:"ans2"}


I want to convert array to object in javascript like [["key1","ans1"],["key2","ans2"]] => {key1:"ans1",key2:"ans2"}


Solution

  • Here you go;

        let res = {}
        let arr = [["key1","ans1"],["key2","ans2"]]
        for (let i = 0; i < arr.length; i++) {
            res[arr[i][0]] = arr[i][1]
        }
    
        console.log(res)

    please comment if you need explanation.