简体   繁体   English

在 React 组件的 function 内部循环

[英]Loop Inside function of React Component

(React web app development) In order to check if the current stock status of products, I use ID of products to loop through json data. (React web 应用开发)为了检查产品的当前库存状态,我使用产品的 ID 循环遍历 json 数据。 I am trying to retrieve value of "DATAPAYLOAD" by key (id) from json (below).我正在尝试通过键(id)从 json(下)检索“DATAPAYLOAD”的值。 idsent is string passed from another component. idsent 是从另一个组件传递的字符串。 But "if (Data.response[i].id === idsent)" this condition always appears to be false because I got "failed" in console.但是“if (Data.response[i].id === idsent)”这个条件似乎总是错误的,因为我在控制台中“失败”了。 That would be really helpful if someone could take a look at the following code and give me some sujections, thanks in advance!如果有人可以查看以下代码并给我一些建议,那将非常有帮助,在此先感谢!

onButtonClicked = () => {
        const idsent="D56F36C6038DFC8244F"
        for (var i = 0; i < Data.response.length; i++) {
            if (Data.response[i].id === idsent) {
                name = Data.response[i].DATAPAYLOAD;
                const word = '<INSTOCKVALUE>INSTOCK</INSTOCKVALUE>';
                if (name.includes(word)) {
                    return true;
                }
                else {
                    return false;
                }
            }
            console.log("failed");
        }

The following is part of the json data that is requested through fetch get-method.以下是通过 fetch get-method 请求的 json 数据的一部分。

Data=    {
    "code": 200,
    "response": [
        {
            "id": "CED62C6F96BD0E21655142F",
            "DATAPAYLOAD": "<AVAILABILITY>\n  <CODE>200</CODE>\n  
            <INSTOCKVALUE>OUTOFSTOCK</INSTOCKVALUE>\n</AVAILABILITY>"
        },
        {
            "id": "D56F36C6038DFC8244F",
            "DATAPAYLOAD": "<AVAILABILITY>\n  <CODE>200</CODE>\n  
            <INSTOCKVALUE>LESSTHAN10</INSTOCKVALUE>\n</AVAILABILITY>"
        },
        {
            "id": "4536C9E608B563A749",
            "DATAPAYLOAD": "<AVAILABILITY>\n  <CODE>200</CODE>\n  
             <INSTOCKVALUE>INSTOCK</INSTOCKVALUE>\n</AVAILABILITY>"
        },
        {
            "id": "3A576872130625CABFADEE68",
            "DATAPAYLOAD": "<AVAILABILITY>\n  <CODE>200</CODE>\n  
            <INSTOCKVALUE>INSTOCK</INSTOCKVALUE>\n</AVAILABILITY>"
        }
    ]
}

Thank you again.再次感谢你。

You probably wanted console.log("failed");你可能想要console.log("failed"); outside of the for loop like the following (so that it only executes once all the data is processed):for循环之外,如下所示(以便它仅在处理完所有数据后执行):

onButtonClicked = () => {
        const idsent="D56F36C6038DFC8244F"
        for (var i = 0; i < Data.response.length; i++) {
            if (Data.response[i].id === idsent) {
                name = Data.response[i].DATAPAYLOAD;
                const word = '<INSTOCKVALUE>INSTOCK</INSTOCKVALUE>';
                if (name.includes(word)) {
                    return true;
                }
                else {
                    return false;
                }
            }
        }
        console.log("failed");

When the fetch is successful, You need to read and parse the data using json().获取成功后,需要使用 json() 读取并解析数据。 Pleas read this请读这个

    onButtonClicked = async () => {
        const idsent="D56F36C6038DFC8244F"
        Data = await Data.json();  // json() will create a promise
        for (var i = 0; i < Data.response.length; i++) {
            if (Data.response[i].id === idsent) {
                name = Data.response[i].DATAPAYLOAD;
                const word = '<INSTOCKVALUE>INSTOCK</INSTOCKVALUE>';
                if (name.includes(word)) {
                    return true;
                }
                else {
                    return false;
                }
            }
            console.log("failed");
        }

The reason you get failed, is because the first time through, the ID does not match the one sent, so it console logs the "failed" message.您失败的原因是因为第一次通过时,ID 与发送的 ID 不匹配,因此控制台会记录“失败”消息。 Then the second time through the for loop it matches the data, and then hits the next if, which checks for the value.然后第二次通过 for 循环匹配数据,然后点击下一个 if,它检查值。 Since the value you are searching for is included in the data, it returns true and the for loop is exited.由于您要搜索的值包含在数据中,因此它返回 true 并退出 for 循环。 The reason you see the fail log is because you are logging when the id doesn't match and there are 3 records in that array where the id don't match, the first one being one of them.您看到失败日志的原因是因为您在 id 不匹配时进行记录,并且该数组中有 3 条 id 不匹配的记录,第一个是其中之一。

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

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