Follow this Example
const elements = [
{ label: "Apple", link: "../fruits" },
{ label: "BMW", link: "../vehicles" }
];
const newObject = Object.fromEntries(elements.map(item => [item.label, item.link]));
console.log(newObject);
Alternatively, you can define a function like so
const elements = [
{ label: "Apple", link: "../fruits" },
{ label: "BMW", link: "../vehicles" }
];
const convertArrayToMap = (array, key) => {
const initialValue = {};
return array.reduce((obj, data) => {
return {
...obj,
[data[key]]: data.link,
};
}, initialValue);
};
console.log(convertArrayToMap(elements, "label"));