简体   繁体   中英

Issues looping through a list of arrays with null data

This is my first question here, hoping you can help. Currently I am trying to loop through an API list of 100 arrays all of which contain one string of data. My loop filters through for numerical data and prints it to a div id. However when I hit data with "#N/A" instead of digits, it breaks my loop. I have tried nesting an if statement that would check if data is null or not, but as it treats null data as an object, this does not work. I have included commented out code to show the things I have tried:

var xhr = new XMLHttpRequest();

var URL = "https://spreadsheets.google.com/feeds/list/0AhySzEddwIC1dEtpWF9hQUhCWURZNEViUmpUeVgwdGc/1/public/basic?alt=json";

xhr.open("GET", URL, false);
xhr.send();

var statusResponseStringify = JSON.stringify(xhr.responseText, "", 2);
var statusResponseParse = JSON.parse(xhr.responseText);
var Find = statusResponseParse.feed.entry;

for (var i = 0; i < Find.length; i++) {


var FTSEContent = statusResponseParse.feed.entry[i].content.$t;
document.getElementById("FTSEName").innerHTML+=FTSEContent + "<br><br>";

var text = FTSEContent;

var value = text.match(/(\d[\d\.]*)/g);

//var price = value[0];
//var change = value[1];

console.log(value);


/*if (typeof value === "number") {
document.getElementById("Change").innerHTML+=value + "<br>";
}
else if (typeof value === null) {
document.getElementById("Change").innerHTML+="N/A" + "<br>";
}
else if (typeof value === "object") {
document.getElementById("Change").innerHTML+="Smell" + "<br>";
}
else {
document.getElementById("Change").innerHTML+="poo" + "<br>";
};*/


if (typeof value == "undefined") {
document.getElementById("Print").innerHTML+="N/A" + "<br>";
}
else {
document.getElementById("Print").innerHTML+=value[0] + "<br>";
};

};

This is the console I get back when I run this code

Could anyone help me with some code ideas to circumvent the null responses when looping. I would ideally like to print the numbers and print an N/A whenever there is a null or #N/A within the API data.

Thank you all!

Rewrite your check: instead of if (typeof value == "undefined") it should be...

if (value === null) { ... }

... as .match() returns null on non-matching, and not undefined .

As a sidenote, your code can be simplified a bit. First, you don't have to repeat the whole statusResponseParse.feed.entry ... expression in FTSEContent, use Find instead:

var FTSEContent = Find[i].content.$t;

Second, my understanding is that you check for number in that content string. In this case, you can adjust your pattern a bit:

var value = FTSEContent.match(/(\d+(?:\.\d+)?)/);

... so it won't consume such illegal numbers as '3..' and '3.14.15' (in the last case, only 3.14 will be matched), and doesn't have to match globally (you only process the first result anyway).

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