括号表示法正在访问带有引号的对象

时间:2019-05-22 18:06:12

标签: javascript object redux

我正在使用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 
    }
  };

这是怎么回事?

1 个答案:

答案 0 :(得分:1)

我正在从串行端口连接中收到action.payload.position

有一个\r并没有显示在字符串的末尾...使用.trim()可以删除它!

更多信息,请参见:

Why are these two identical strings not equal in JavaScript?