简体   繁体   English

根据匹配后的计算,在对象数组内设置对象属性的状态:reactjs

[英]Setting state of an object property inside array of objects based on a computation after match : reactjs

https://codesandbox.io/s/polished-bird-662mh?from-embed https://codesandbox.io/s/polished-bird-662mh?from-embed

I am having this an array of objects and an object with the following structure 我有一个对象数组和一个具有以下结构的对象

this.state = {
               arr : [ {
                        id: "val1",
                        initialVal: "test1",
                        finalVal: "final1"
                       },
                       {
                         id: "val2",
                         initialVal: "test2",
                         finalVal: "final2"
                       },
                       {
                         id: "val3",
                         initialVal: "test3",
                         finalVal: "final3"
                       },
                       {
                         id: "val4",
                         initialVal: "test4",
                         finalVal: "final4"
                       }
                     ],
                values: [ "test1","test3"]
              }

this.obj = { field: "test1" , val:6}

I am writing a function that takes this obj as its parameter and based on the "field" value it should set the state of "finalVal" with the "val" property of obj with the following computation(if val is greater than 5 add "ok" to the field else add "cancel") and the objects whose property don't match in the "values" array of the state its "finalVal" should be set to just blank 我正在编写一个函数,将该obj作为其参数,并根据“字段”值,应通过以下计算将obj的“ val”属性设置为“ finalVal”状态(如果val大于5,则添加“确定”到该字段,否则添加“取消”),其属性在状态的“值”数组中不匹配的对象的“ finalVal”应设置为空白

So the output should look after setting state : 因此输出应注意设置状态:

 [ 
             {
              id: "val1",
              initialVal: "test1",
              finalVal: "ok"
             },
             {
              id: "val2",
              initialVal: "test2"
              finalVal: "final2"
             },
             {
              id: "val3",
              initialVal: "test3"
              finalVal: ""
             },
             {
              id: "val4",
              initialVal: "test4"
              finalVal: "final4"
             }
 ]


//What I have tried so far

 setObjState = obj => {
    let arr = [...this.state.arr];
    let finalArr = arr.map(function(item) {
      if (item.initialVal === obj.field) {
        return { ...item, finalVal: obj.val > 5 ? "Ok" : "Cancel" };
      } else {
         if(this.state.values.includes(item.id){
              return { ...item, finalVal: "" };
          }
      }
    });
    console.log(finalArr);
    this.setState({ arr: finalArr });
  }

You were missing an else condition: 您缺少else条件:

setObjState = obj => {
  let arr = [...this.state.arr];
  let finalArr = arr.map(item => {
    if (item.initialVal === obj.field) {
      return { ...item,
        finalVal: obj.val > 5 ? "Ok" : "Cancel"
      };
    } else {
      if (this.state.values.includes(item.id)) {
        return { ...item,
          finalVal: ""
        };
      } else {
        return { ...item
        };
      }
    }
  });
  console.log(finalArr);
  this.setState({
    arr: finalArr
  });
};

Here's a Working CodeSandbox Sample for your ref. 这是供您参考的工作CodeSandbox示例

You can simplify that function a lot with: 您可以使用以下方法大大简化该功能:

  setObjState = obj => {
    // create a new Array of items

    const arr = this.state.arr.map(item => {
      // determine the value
      const value = obj.val > 5 ? 'ok' : 'cancel';

      // set value or empty value
      const finalVal = item.initialVal === obj.field ? value : '';

      // return new object with finalVal
      return {
        ...item,
        finalVal,
      };
    });

    this.setState({ arr });
  };

https://codesandbox.io/s/gifted-frost-4olqb https://codesandbox.io/s/gifted-frost-4olqb

You need a final else block in the event that none of the if-conditions pass, just so that you can return the item as-is: 如果没有通过if条件,则需要一个final else块,只是为了可以按原样返回该项目:

I also think you're trying to check for item.initialVal since that actually has the test numbers. 我还认为您正在尝试检查item.initialVal,因为它实际上具有测试编号。

  setObjState = obj => {
    let arr = [...this.state.arr];
    let finalArr = arr.map(item => {
      if (item.initialVal === obj.field) {
        return { ...item, finalVal: obj.val > 5 ? "Ok" : "Cancel" };
      } else {
        if (this.state.values.includes(item.initialVal)) {
          return { ...item, finalVal: "" };
        } else {
          return item;
        }
      }
    });
    console.log(finalArr);
    this.setState({ arr: finalArr });
  };

See sandbox: https://codesandbox.io/s/falling-currying-no6ui 请参阅沙箱: https : //codesandbox.io/s/falling-currying-no6ui

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM