Two objects are given const en = ["mon", "tue", "wed", "thu", "fri", "sat", "sun"]; const de = ["montag", "Dienstag", "Mittwoch", "Donnerstag", "Freitag", "Samstag", "Sonntag"];
It is necessary to combine these 2 arrays into one object so that the values of the first array are keys, and the values of the second array are values. How to do it?
You can do it using Object.fromEntries.
const
en = ["mon", "tue", "wed", "thu", "fri", "sat", "sun"],
de = ["montag", "Dienstag", "Mittwoch", "Donnerstag", "Freitag", "Samstag", "Sonntag"],
result = Object.fromEntries(en.map((k, i) => [k, de[i]]));
console.log(result);
You can also reduce one of the arrays into the desired object.
const
en = ["mon", "tue", "wed", "thu", "fri", "sat", "sun"],
de = ["montag", "Dienstag", "Mittwoch", "Donnerstag", "Freitag", "Samstag", "Sonntag"],
result = en.reduce((acc, k, i) => ({ ...acc, [k]: de[i] }), {});
console.log(result);
Another solution using map, easy to understand:
const en = ["mon", "tue", "wed", "thu", "fri", "sat", "sun"];
const de = ["montag", "Dienstag", "Mittwoch", "Donnerstag", "Freitag", "Samstag", "Sonntag"];
let result = {};
en.map((item, index) => result[item] = de[index])
Iterate over over the first array and collect the elements of the second at th e matching index:
const en = ["mon", "tue", "wed", "thu", "fri", "sat", "sun"];
const de = ["montag", "Dienstag", "Mittwoch", "Donnerstag", "Freitag", "Samstag", "Sonntag"];
let dict={}
;
en.forEach ( (px, pn) => { dict[px] = de[pn]; });
console.log(dict);