Consider the following two arrays:
let values = ["52", "71", "3", "45", "20", "12", "634", "21"];
let names = ["apple", "orange", "strawberry", "banana", "coconut", "pineapple", "watermelon", "plum"];
Is there a way to combine them into an object like this:
{
"apple": 52,
"orange": 71,
"strawberry": 3,
"banana": 45,
"coconut": 20,
"pineapple": 12,
"watermelon": 634,
"plum": 21
}
I experimented with Object.assign
but it only changed the values.
Object.assign<any, any>(names, values);
I also attempted using Object.defineProperties
, but I couldn't get it to work, or maybe I just don't understand how to properly use it.
UPDATE
I gave this approach a try:
let temp = {};
names.forEach((item, index) => {
console.log('item: ', item);
console.log('index: ', index);
console.log('temp[item]: ', temp[item]);
console.log('values[index]: ', values[index]);
temp[item] = values[index];
console.log(names);
});
But unfortunately, it didn't produce the desired result.