0

Having two arrays:

let values = ["52", "71", "3", "45", "20", "12", "634", "21"];
let names = ["apple", "orange", "strawberry", "banana", "coconut", "pineapple", "watermelon", "plum"];

How can I create an object like:

{
    "apple": 52,
    "orange": 71,
    "strawberry": 3,
    "banana": 45,
    "coconut": 20,
    "pineapple": 12,
    "watermelon": 634,
    "plum": 21
}

I tried using Object.assign but this only overrides the values.

Object.assign<any, any>(names, values);

Object.defineProperties doesn't work as well or - more likely - I don't know how to use them.

EDIT

I tried the

    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 this is what I got

console logs

big_OS
  • 381
  • 7
  • 20

1 Answers1

1

Create a new result-object. Add to this object foreach element of names a nnew object with name and corresponding value.

Edited: Because it seems tht you have in your values-array all elements as string and in your result-object the values are integers I use parseInt for convertion.

Note: Using a variable name is not so good because you get an TypeError if you want to use anything like this name.forEach, name.map. It seems it's a reserved word or something like this.

let values = ["52", "71", "3", "45", "20", "12", "634", "21"];
let names = ["apple", "orange", "strawberry", "banana", "coconut", "pineapple", "watermelon", "plum"];

let temp = {};
names.forEach((elem, index) => { temp[elem]=parseInt(values[index]) });

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);
});
Sascha
  • 4,576
  • 3
  • 13
  • 34
  • I tried this solution but this doesn't work for me. I updated the question, please have a look and let me know what you think – big_OS Aug 17 '20 at 10:54
  • I edited my solution: Now the values in the result are integers and not strings. – Sascha Aug 17 '20 at 11:14
  • I understand, but theres other thing: as my `temp` in the question edit is yours `result` here when I try to reach the `result[elem]` it turns out that it's undefined. I don't know why. – big_OS Aug 17 '20 at 11:48
  • disclaimer: array names I used here are just to ease things up, in the real app they are named differently ;) – big_OS Aug 17 '20 at 11:51
  • I asked for ths behaviour https://stackoverflow.com/questions/63450022/without-let-before-name-typeerror-name-map-is-not-a-function?noredirect=1#comment112197912_63450022 just now. Look at the first comment: `name` without declaration refers to `window.name` property, which is type-protected (it's always a string). – Sascha Aug 17 '20 at 11:59
  • sorry, my bad - I forgot to put `let`'s before each declaration, let me fix that – big_OS Aug 17 '20 at 12:01
  • but this doesn't fix the issue of `undefined` :( – big_OS Aug 17 '20 at 12:15
  • 1
    Yes because you use `temp` and you didn't set any properties for it. So you had to use the same variable in your output-loop and for your created object. I changed the code by renaming result to temp and everything works fine. Look the code above. – Sascha Aug 17 '20 at 12:24