如何检查JSON对象数组的每个项目的特定值?

时间:2019-07-04 11:24:44

标签: arrays json reactjs object foreach

我正在使用REACT JS。我的JSON对象数组的格式如下:

{
        "records": {
            "Master Automotives": [
                {
                    "SparePartID": "43",
                    "Name": "Oil and Lubricants",
                    "err": ""
                },
                {
                    "SparePartID": "45",
                    "Name": "Lights",
                    "err": "Stock Error"
                }
            ],
            "Repair Solutions": [
                {
                    "SparePartID": "47",
                    "Name": "Steering Wheel",
                    "err": ""
                }
            ],
            
             "FiveStar Automotives": [
                {
                    "SparePartID": "51",
                    "Name": "Brakes",
                    "err": ""
                },
                {
                    "SparePartID": "53",
                    "Name": "Clutch",
                    "err": "Stock Error"
                },
                  {
                    "SparePartID": "55",
                    "Name": "LED",
                    "err": ""
                }
            ]
        }
    }

现在,在一种名为 placeOrder ()的方法中,我想检查以上JSON中是否有列“ err ”值=“股票错误”的项目,如果任何一个或多个项目具有 err 值,我想将 check 的状态设置为true。

但是所有项目的err值都等于null,即 err =“” ,然后应将 check 的State设置为 false

我在下面尝试了此代码,但没有用。你能告诉我我做错了什么吗?

placeOrder()
{

const arr = {...this.state.myrecords};

Object.keys(arr).forEach(key => {
   if (arr[key].err === 'Stock Error')
    {
     this.setState({
              check: true
        })
       }
   })
   
}

2 个答案:

答案 0 :(得分:1)

如果我正确理解,则落后了一步,即您正在访问arr[key],这只会为您提供内部数组。

那是

[
    {
        "SparePartID": "43",
        "Name": "Oil and Lubricants",
        "err": ""
    },
    {
        "SparePartID": "45",
        "Name": "Lights",
        "err": "Stock Error"
    }
]`

您需要检查err的那些数组元素。

arr[key][0].err === 'Stock Error'

这将检查第一个索引。 您可以在数组上进一步实现map函数,以检查所有元素。

答案 1 :(得分:1)

看看这个正则表达式解决方案:

const obj = {
        "records": {
            "Master Automotives": [
                {
                    "SparePartID": "43",
                    "Name": "Oil and Lubricants",
                    "err": ""
                },
                {
                    "SparePartID": "45",
                    "Name": "Lights",
                    "err": "Stock Error"
                }
            ],
            "Repair Solutions": [
                {
                    "SparePartID": "47",
                    "Name": "Steering Wheel",
                    "err": ""
                }
            ],
            
             "FiveStar Automotives": [
                {
                    "SparePartID": "51",
                    "Name": "Brakes",
                    "err": ""
                },
                {
                    "SparePartID": "53",
                    "Name": "Clutch",
                    "err": "Stock Error"
                },
                  {
                    "SparePartID": "55",
                    "Name": "LED",
                    "err": ""
                }
            ]
        }
    };

const objString = JSON.stringify(obj);
const regPattern = /\bStock Error\b/;
const result = objString.match(regPattern);
console.log(result); // This will log the first "stock error" in your object so you know if there is one

用法:

如果result.length> 0 //至少存在1个库存错误 如果result.length === 0 //不存在库存错误