【问题标题】:how to update all keys with the same name in a array of objects?如何更新对象数组中具有相同名称的所有键?
【发布时间】:2021-11-24 09:29:17
【问题描述】:

我正在使用 react 和 redux,并希望将每个 'volume' 键值减少 0 到 20 之间的随机数(均包括在内),然后更新状态数组。

我的数组是这样的:

const arr = [
  {id: 1, volume: 100, isEmpty: false},
  {id: 2, volume: 100, isEmpty: false}
]

这是我的行动:

const decrementAction = () => {
    return {
        type : 'DECREMENT'
    }
}

我的减速器是这样的:

const containers = (state = arr, action) => {
  switch(action.type){
    case 'DECREMENT':
      const randomNumber = Math.floor(Math.random() * 20) + 0;
      const newArr = arr.map(item => {
        const newObj = {
          ...item,
          volume : item.volume - randomNumber
        }
        return newObj;
      });
      state = newArray
  }
};

我在运行我的代码时遇到了几个问题:

1 - 每个值都是相同的,而不是为每个对象获取一个随机值(已解决)

2 - 值同时递增和递减,而不是仅递减

请问,我做错了什么?

谢谢

【问题讨论】:

    标签: reactjs redux state


    【解决方案1】:

    考虑在你的减速器中在state = newArray 之后放置一个break;,这至少应该解决你的第二个问题:

     const containers = (state = arr, action) => {
      switch(action.type){
        case 'DECREMENT':
          const randomNumber = Math.floor(Math.random() * 20) + 0;
          const newArr = arr.map(item => {
            const newObj = {
              ...item,
              volume : item.volume - randomNumber
            }
            return newObj;
          });
          state = newArray;
          break;
      }
    };
    

    Javascript 开关参考:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/switch

    【讨论】:

    • 谢谢你,我已经尝试过你的建议,但没有解决我的问题。
    【解决方案2】:

    首先,您只获得一次随机数。您每次都需要一个新的随机数。

    尝试在循环中运行整个事情,并通过像您当前所做的那样解构它们来继续添加项目。

    const containers = (state = arr, action) => {
      switch(action.type){
        case 'DECREMENT':
          let newArr = [];
          for(let i = 0 ; i < arr.length; i++) {
          const randomNumber = Math.floor(Math.random() * 20) + 0;
           const newObj = {
              ...arr[i],
              volume : arr[i].volume - randomNumber
            };
          newArr.push(newObj);
          }
          return newArr;
          break;
       
       }
    };
    
    

    我也相信使用 redux 你应该返回状态。

    【讨论】:

    • 谢谢你,你对 randomNumber 的看法是对的!
    • 没有问题。如果这解决了您的问题,请使用绿色勾号接受答案
    • 我的问题只是部分解决了,所以我没有使用绿色的勾
    • 好的。请让我知道目前有什么不工作
    • 刚刚改了代码。 item 是个错误。我希望你使用arr[i]。如果这不能解决问题,您可以添加一个分钟。复制示例
    【解决方案3】:

    我找到了解决办法...

    而不是

    const containers = (state = arr, action) => {
      switch(action.type){
        case 'DECREMENT':
          const randomNumber = Math.floor(Math.random() * 20) + 0;
          const newArr = arr.map(item => {
            const newObj = {
              ...item,
              volume : item.volume - randomNumber
            }
            return newObj;
          });
          state = newArray
      }
    };
    

    第 5 行我将状态称为“arr”,但我应该将状态称为“状态”,如下所示。

    const containers = (state = arr, action) => {
      switch(action.type){
        case 'DECREMENT':
          const randomNumber = Math.floor(Math.random() * 20) + 0;
          const newArr = state.map(item => {
            const newObj = {
              ...item,
              volume : item.volume - randomNumber
            }
            return newObj;
          });
          state = newArray
      }
    };
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-05-01
      • 1970-01-01
      • 2020-08-23
      • 2022-11-11
      • 2022-01-18
      • 2021-02-12
      相关资源
      最近更新 更多