As @PCDSandwichMan mentioned you can use reduce. See:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reduce
Reduce goes through the array and returns a single value. It can sometimes be littly tricky to see what is going on although it looks nice as a one-liner
If you want to see a more transparent method you can start with empty array, loop through the array and add a property to the object for each element in the array.
const keys = ["J", "o", "h", "a", "n"];
const values = [1, 1, 1, 2, 1];
const myObj = {}
keys.forEach((key, index) => {
myObj[key] = values[index]
})
console.log(myObj)
Here we go through the keys
array, each element in the keys
array is named key
.
We then use the brack notation of an object to create a property and assign the value from the values
array that is at the same index/location as the key element.