简体   繁体   English

对更改检查是否等于对象数组做出反应

[英]react on change check if equal to array of object

i have input and array of object i need when i type it will display the object.我有输入和我需要的对象数组,当我输入它会显示该对象。 "airplaneCompany" is the object property that i need to compare i was doing only if the input is equal to the "airplaneCompany" it will return it by the filter method but i need for evrey char it will check it and if the object start with "a" it will show this object “airplaneCompany”是我需要比较的对象属性,只有当输入等于“airplaneCompany”时,它才会通过过滤器方法返回它,但我需要 evrey char,它会检查它,如果对象以"a" 它将显示这个对象

  const [txtInp, setTxtInp] = useState("");
  const showFlight = users.filter((user) => {
    return user.airplaneCompany == txtInp;
  });

     {showFlight.map((user, index) => {
        const { id, airplaneCompany, passenger } = user;
        return (
          <div className="flightContainer" key={index}>
            <div>{id}</div>
            <div>{airplaneCompany}</div>
            <div>{passenger}</div>
          </div>
        );
      })}

I think you can use your .filter function to check if the airplaneCompany starts with the user input?我认为您可以使用 .filter 函数来检查飞机公司是否从用户输入开始?

Something like就像是

return user.airplaneCompany.indexOf(txtInp) === 0;返回 user.airplaneCompany.indexOf(txtInp) === 0;

You can use @Patrick answer, but JavaScript has its own startsWith function you can use.您可以使用@Patrick 答案,但 JavaScript 有自己的startsWith函数可以使用。

Also, consider wrapping the filter with the useMemo hook to run it only when the input changes and not on every render.此外,请考虑使用useMemo挂钩包装过滤器,以便仅在输入更改时运行它,而不是在每次渲染时运行。

const showFlight = useMemo(() => {
  return users.filter((user) => {
    return user.airplaneCompany == txtInp;
  });
}, [txtInp]);

just use regex.只需使用正则表达式。 just place input value like /^airplaneCompany$/只需放置输入值,例如/^airplaneCompany$/

 const wrongInputText = 'q' const rightInputText = 'airplaneCompany' console.log('wrong', 'return value=', /^airplaneCompany$/.test(wrongInputText)) console.log('right', 'return value=',/^airplaneCompany$/.test(rightInputText))

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

相关问题 检查 object 数组中的所有值是否等于 false 并在 react 中返回 true - Check if all the values in array of object is equal to false and return true in react 循环进入数组对象然后检查是否有相等的值? - Loop into array object then check if there's a equal value? 如何检查 object 值是否等于数组中的字符串? - How to check if an object value is equal to a string in an array? 检查 object 数组中键的每个值是否相等或 null - Check if each value of a key in an object array are equal or null Javascript-当两个数组包含一个对象和一个数组时,检查两个数组是否相等 - Javascript - Check if two arrays are equal when arrays contain an object and an array 使用多个条件检查数组中的任意两个 object 是否相等 - Check any two object in an array are Equal using multiple Conditions 检查存储在数组中的 object 中的用户 ID 是否等于经过身份验证的用户的 ID - Check if the User Id stored in an object in an array is equal to the Id of the Authenticated user 如何检查单击的元素是否等于数组中的对象? - How to check if the clicked element is equal to an object from an array? 如何在 React 中检查 object 数组是否存在于 state 中? - How to check if array of object exist in state in React? 需要在 React.js 中检查 object 数组 - Need to check array of object in React.js
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM