本文最后更新于193 天前,其中的信息可能已经过时,如有错误请发送邮件到1986413837@qq.com
const countObj = {}
candidates.forEach((item, index) => {
console.log(index, item)
Object.defineProperty(countObj, String(index), {
value:item
})
})
console.log(countObj)
//{}
//为什么为空数组?
默认的 enumerable是 false,所以属性不会在 console.log中显示

加上enumerable即可
candidates.forEach((item, index) => {
Object.defineProperty(correctObj, String(index), {
value: item,
enumerable: true,
})
})
console.log('正确写法:', correctObj) // {0: 'a', 1: 'b', 2: 'c'}