简体   繁体   中英

How can I grab each value from an Array and compare against a hard coded value?

Please review the below code:

    var textArray = new Array();
    var allText = results.data._contained.text;
    for (var i = 0; i < allText.length; i++) {
        var text1 = allText[i];
        var textHtml = "<div id='text_item'>";
        textHtml += "<span class='some_div'>{0}</span>".replace("{0}", text1.text_is);
        textHtml += "</div>";
        textArray.push(text1.texts.priority);

        $("#text_box").append(textHtml);
    }

    if (foo === 'Some text') {
        document.write("match");
    } else {
        document.write("not match");
    }

}

In the code above, I am getting values in an array. And then below that I have this if/else statement. How can I grab one value at a time from the array and compare against "some text"?

You mean?

for (var i = 0; i < allText.length; i++) {
    var text1 = allText[i];
    if (text1 === 'Some text') {
        document.write("match");
    } 
    else {
        document.write("not match");
    }
    ....
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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