如何在reactjs中以不可变的方式更新嵌套数组值

时间:2017-08-19 08:50:08

标签: javascript reactjs ecmascript-6 immutability

我正在研究reactjs

[
    {
        key: 'a',
        map: [
            {
                brand: 'sam',
                year: 2015,
                models: [
                    {
                        pk: 1,
                        value: 15,
                    },
                    {
                        pk: 2,
                        value: 20,
                    },
                ],
            },
            {
                brand: 'sony',
                year: 2016,
                models: [
                    {
                        pk: 3,
                        value: 15,
                    },
                    {
                        pk: 4,
                        value: 20,
                    },
                ],
            },
        ],
    },
    {
        key: 'b',
        map: [
            {
                brand: 'nok',
                year: 2015,
                models: [
                    {
                        pk: 1,
                        value: 15,
                    },
                    {
                        pk: 2,
                        value: 20,
                    },
                ],
            },
            {
                brand: 'folo',
                year: 2016,
                models: [
                    {
                        pk: 3,
                        value: 15,
                    },
                    {
                        pk: 4,
                        value: 20,
                    },
                ],
            },
        ],
    },
]

现在我的任务是value属性的用户更新值,所以我必须更新我的整个数组。我正在努力以不可改变的方式做到这一点。已编写代码以这种方式更新

let groupB = [...this.state.groupB];

  const modifiedPrgram = groupB.filter(data => data.brand === obj.brand)
  .map(proData => proData.map.filter(filetredData => filetredData.year === this.props.year))
  let models=modifiedPrgram[0].map(data => data.models)

  var selectedData = models.filter(data => data.pk === obj.pk);

    var diff = obj.value - selectedData[0].value;

    var otherData = models.filter(data => data.pk !== obj.pk);

    var sum = otherData.map(data => data.value).reduce((a, b) => a + b, 0);

    for (var i = 0; i < models.length; i++) {
      if (models[i].pk !== obj.pk) {
        models[i].value =
          models[i].value -( models[i].value / sum )* diff;
      } else 
        models[i].value = obj.value;

    }

但现在很困惑如何在我的groupB中重新添加该值。请在这个建议我

2 个答案:

答案 0 :(得分:0)

通常,您可以使用以下代码更改每个组的值:

// afterwards you receive a new Group in the same format as before!
const newGroup = groups.map(group => ({
    ...group,
    map: group.map.map(branding => ({
        ...branding,
        models: branding.models.map(model => ({ 
         ...model, 
         value: 10 /* Change the value over here! */ 
      })),
    })),
}))

这显然是非常嵌套的,不太可读,但它是不可变的。 下次我建议使用像immutable.js这样的库或其他东西!

答案 1 :(得分:0)

这是不可变数组的另一个例子

case MOVE_DEVICE_SUCCESS:
  console.log(action.payload);
  let Groups = [...state];
  let idxGroup = Groups.findIndex(group => {
    return group.id === action.payload.GroupId;
  });

  let idxDevice = Groups[idxGroup].Devices.findIndex(device => {
    return device.id = action.payload.id;
  });

  let temp = {...Groups[idxGroup].Devices[idxDevice]};

  let newOne = [...Groups.map(group => {
    if (group.id === action.payload.GroupId){
      const filtered = group.Devices.filter(device => {
        if(device.id !== action.payload.id){
          return {...device}
        }
      });
      group.Devices = [...filtered];
      return {...group}
    } else if (group.id === action.payload.TargetGroupId){
      group.Devices = [...group.Devices, {...temp}];
      return {...group}
    } else {
      return {...group}
    }
  })]
  return [...newOne];

default:
  return state