I discovered many asked questions like this. Unfortunately couldn't find the right solution. If this question is duplicated, please be gentle and let me know.
Describe the issue: I have two objects like below:
const letters = {
0: "A",
1: "B",
2: "C",
3: "D"
};
const numbers = {
0: 47,
1: 48,
2: 37,
3: 29
};
I want to merge them like keys and values:
let result = {
A: 47,
B: 48,
C: 37,
D: 29
};
Is it available to achieve that with shorter ways?
const letterArray = Object.assign([], letters), numberArray = Object.assign([], numbers), result = Object.fromEntries(letterArray.map((letter, index) => [ letter, numberArray[index] ]));.transpose = (matrix) => matrix[0].map((_, colIndex) => matrix.map(row => row[colIndex])), letters = ['A', 'B', 'C', 'D'], numbers = [47, 48, 37, 29], result = Object.fromEntries(transpose([letters, numbers]));Object.entries(letters).reduce((acc, [k,v]) => ({...acc, [v]: numbers[k]}), {});