如何在react中过滤具有给定值的状态对象

问题描述 投票:0回答:1

我只是想根据value(状态中存在的变量)过滤状态产品对象项,>

我已经尝试从API获取所有产品并以产品状态存储

这是我的状态

state = {
        isLoading: false,
        results: [],
        value: '',
        source: [],
        products: [],
        title:'',
        desc:'',
        img: '',
        price: '',
        temp:''

    }

我只想用值过滤数组对象乘积。

假设下面是我的产品对象状态

{"foodProductsId":4001,"subCategoryId":3024,"productName":"Angus Beef(500 gms)","brand":"Angus","price":240.0,"rating":4.5,"imageUrl":"https://cdn.pixabay.com/photo/2018/02/08/15/02/meat-3139641_960_720.jpg","quantity":118,"description":" Angus beef is beef taken from an Angus cow. Angus is a breed of cattle with its own unique characteristics. Angus cattle are known for having genes that make its meat more tender, marbled, and flavorful than regular beef. The Certified Angus Beef  brand is the best Angus brand available. It's a cut above USDA Prime, Choice and Select. Ten quality standards  including abundant marbling, ensure every bite is exceptionally flavorful, incredibly tender and naturally juicy."},{"foodProductsId":4002,"subCategoryId":3024,"productName":"Strauss Veal(250 gms)","brand":"Strauss","price":300.0,"rating":4.3,"imageUrl":"https://cdn.pixabay.com/photo/2018/04/12/11/43/veal-chop-3313222_960_720.jpg","quantity":111,"description":"Strauss veal: Our calves are never tethered and raised on a diet containing more iron, which is essential for developing a healthy immune system. Freedom of movement promotes good health and reduces stress, which helps to ensures tenderness. It's good for the calves and good for you."}

我只是想根据产品名称获得foodProductId

例如:value =安格斯牛肉(500 gms)我应该得到ID:4001

请对此查询提供帮助。

提前感谢

我只是想根据值(状态中存在的变量)过滤状态产品对象项,我试图从API获取所有产品并存储在产品状态中,这是我的...

reactjs filter semantic-ui
1个回答
0
投票
您可以使用Array.find(),根据Array.find()查找对象。 value将从数组返回第一个匹配的对象,然后您可以从该对象获取Array.find()
© www.soinside.com 2019 - 2024. All rights reserved.