Blog

In JavaScript Create an Object with Property Name/Value Pairs from Arrays
Posted on June 9, 2015 in Algorithms, JavaScript by Matt Jennings

var keys = ["first_name", "last_name", "email", "age", "address"];
var values = ["elon", "musk", "", 35, "mars"];

function buildObject(nkeys, nvalues) {
    var newObject = {};

    for(var i = 0; i < keys.length; i++) {
        newObject[nkeys[i]] = nvalues[i];

    }

    return newObject;
}

console.log(buildObject(keys, values));

Leave a Reply