2822. 对象反转
2025年6月25日小于 1 分钟
2822. 对象反转
var invertObject = function (obj) {
const result = {}
for (const [key, value] of Object.entries(obj)) {
if (value in result) {
if (Array.isArray(result[value])) {
result[value].push(key) // 直接 push,避免创建新数组
} else {
result[value] = [result[value], key]
}
} else {
result[value] = key
}
}
return result
}