在我们日常开发过程中,有时需要将对象的键值对进行互换,这时就可以使用以下此方法:
const companyGroup = {
"3284519712": "chairman",
"3284529360": "generalManager",
"3284424672": "officeDirector",
"3284423712": "manager",
};
const companyGroupMap = Object.entries(companyGroup || {}).reduce(
(acc: any, [key, value]: any) => {
acc[value] = key;
return acc;
},
{}
);
console.log(companyGroupMap);
{chairman: "3284519712",
generalManager: "3284529360",
manager: "3284423712",
officeDirector: "3284424672",
}