我正在使用JavaScript中的bracket notation
访问对象。由于某种原因,它会将值放在引号中……不是我想要的。
console.log(positions)
{
element: null,
elementTwo: null,
elementThree: null,
"elementThree": 1
}
更多信息
在reducer
中,我的状态为redux ...
const initialState = {
positions: {
element: null,
elementTwo: null,
elementThree: null
}
};
我应该能够足够容易地操纵它
case SET_POSITION:
console.log(action.payload.element) //elementTwo
return {
...state,
positions: {
...state.positions,
[action.payload.element]: action.payload.position
}
};
如果我console.log(action.paylod.element)
和console.log('elementThree')
是相同的。
而且,如果我使用以下命令进行访问,则可以使用:
case SET_POSITION:
console.log(action.payload.element) //elementTwo
return {
...state,
positions: {
...state.positions,
['elementThree']: action.payload.position
}
};
这是怎么回事?
答案 0 :(得分:1)
我正在从串行端口连接中收到action.payload.position
。
有一个\r
并没有显示在字符串的末尾...使用.trim()
可以删除它!
更多信息,请参见:
Why are these two identical strings not equal in JavaScript?