简体   繁体   English

更改对象属性以匹配给定数组中的值

[英]change object property to match values in given array

Heres my app structure 这是我的应用程序结构

Codepen Example : https://codesandbox.io/s/awesome-blackwell-kd3nn Codepen示例: https ://codesandbox.io/s/awesome-blackwell-kd3nn

class MasterForm extends Component {
  constructor() {
    super();

    this.state = {
      userInput: "",
      available: "",
      silos: []
    };
  }

  handleFormSubmit = e => {
    e.preventDefault();
    this.silo_1Lookup();
  };

  handleInputChange = e => {
    let input = e.target;
    let name = e.target.name;
    let value = input.value;

    this.setState({
      [name]: value
    });
  };

  silo_1Lookup = () => {
    let silos = [...this.state.silos];

    silos.push({
      userInput: this.state.userInput,
      available: this.state.available
    });

    this.setState({
      silos,
      userInput: "",
      available: ""
    });

    const siloDepthFilling = {
      "0": "926",
      "0.5": "893",
      "1": "860",
      "1.5": "827",
      "2": "794",
      "2.5": "761",
      "3": "728",
      "3.5": "695",
      "4": "662",
      "4.5": "629",
      "5": "595"
    };
    console.log("function ran");
    return siloDepthFilling[silos[0].userInput];
  };
}

I'm trying to change the property of silos[0].available to match the content inside of my silo_1Lookup() function. 我正在尝试更改silos [0]的属性。该属性可匹配silo_1Lookup()函数内部的内容。

For example, if the user enters 5, silos[0].available should be set to "595" 例如,如果用户输入5,则silos [0] .available应该设置为“ 595”

if (silos[0].userInput === '5') {
 return silos[0].available = "595"
}

kinda like that but using the dictionary inside silo_1Lookup(); 有点像,但是在silo_1Lookup()中使用了字典;

At the moment, the function runs, but silos[0].available is not being updated with a new value, and there is no error message 目前,该函数正在运行,但是silos [0] .available没有用新值更新,并且没有错误消息

You are not updating the state after changing available value. 更改available值后,您不会更新状态。

In your function, call setState() after making required changes. 在您的函数中,进行必要的更改后调用setState()

You are using silos to display both userInput and available . 您正在使用silos同时显示userInputavailable So updating silos is what changes the view, not available . 因此更新silos是改变视图的原因,不可available

silo_1Lookup = () => {

    const siloDepthFilling = {
      "0": "926",
      "0.5": "893",
      "1": "860",
      "1.5": "827",
      "2": "794",
      "2.5": "761",
      "3": "728",
      "3.5": "695",
      "4": "662",
      "4.5": "629",
      "5": "595"
    };
    console.log("function ran");

    let available = siloDepthFilling[this.state.userInput];

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

    silos.push({
      userInput: this.state.userInput,
      available: available
    });

    this.setState(
      {
        silos: silos,
        available: available
      }
    );
  };

Also, you are not using key for your list items and it is throwing an error. 另外,您没有将key用于列表项,并且会引发错误。 Make following changes to your Table component. Table组件进行以下更改。

{silos.map((silo, i) => {
     return (
        <tr key={i.toString()}>
           <td>{silo.userInput}</td>
           <td>{silo.available}</td>
        </tr>
      );
 })}

Live demo: https://codesandbox.io/s/vigorous-jennings-wlw8j 现场演示: https : //codesandbox.io/s/vigorous-jennings-wlw8j

The root cause of your problem is, you need to update every time your array silos[] when you will get a new value for available . 问题的根本原因是,每当您的阵列silos[]获得available的新值时,您都需要更新。 so the approach has given below. 因此,方法如下。

  handleInputChange = e => {
    let input = e.target;
    let name = e.target.name;
    let value = input.value;

    this.setState({
      [name]: value
    });
  };

  silo_1Lookup = () => {
    let silosss = [...this.state.silos];
    const siloDepthFilling = {
      "0": "926",
      "0.5": "893",
      "1": "860",
      "1.5": "827",
      "2": "794",
      "2.5": "761",
      "3": "728",
      "3.5": "695",
      "4": "662",
      "4.5": "629",
      "5": "595"
    };

    // Push user input and available value in array
    silosss.push({
      userInput: this.state.userInput,
      available: siloDepthFilling[this.state.userInput]
    });

    // Finally update the state of both array and available value
    for(var item of silosss){
      this.setState({
        silos:silosss,
        available: item.available
      });
    }
  };

Finally, the output should be like that 最后,输出应该是这样的

在此处输入图片说明

暂无
暂无

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

相关问题 将数组值匹配到JavaScript中的对象属性 - Match array values to object property in JavaScript 如果属性与比较值不匹配,如何更改数组中 object 属性的值? - How to change value for object property in array if property not match with compared value? 从给定数组中获取包含动态 object 属性值的数组 - Get an array containing values of dynamic object property from the given array 如何更改数组对象中的属性值? - How to change property values in an array Object? 匹配具有 object 属性的数组 - Match an array with an object property 如何使用 javascript 中的数组值更改数组 object 中的属性值 - How to change property value in array object with array values in javascript 如果属性匹配,则返回数组中的对象 - Return Object in Array if Property Match 如何将对象的属性与对象数组的属性匹配? - How to match a property of an object to a property of an array of objects? 如何将具有相同属性名称的对象数组转换为从给定数组中的值连接值的对象? JS - How to transform an array of objects with the same property name into the object in which value is concatenated from values in a given array? JS Javascript - 当对象的属性值与选项数组中的所有值匹配时,遍历一组选项并返回一个对象? - Javascript - Loop through an array of options and return an object when the object's property values match all values in the options array?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM