i have a response from the API something like this:
var res = [{data: "street_number", label: "121"},
{data: "route", label: "Ladbroke Grove"},
{data: "postal_town", label: "London"},
{data: "administrative_area_level_1", label: "England"},
{data: "postal_code", label: "W11 1PN"}]
but sometimes like so:
var res = [{data: "route", label: "Ladbroke Grove"},
{data: "postal_town", label: "London"},
{data: "administrative_area_level_1", label: "England"},
{data: "postal_code", label: "W11 1PN"}]
so it missed the "street_number".
Now how can i check if that specific data-value exists, regardless of what label-value is? And i don't wanna check on res.length
either, since the length of it is really different.
You need to iterate over the dataset and check for the specific value you need.
var hasStreetNumber = res.find((x) => x.data === 'street_number') !== undefined;
Is one of many ways you check if the value you're looking for exists in the list.
This is an array of JS objects. A simple option would be to iterate through the array and check for the presence of a certain String in each Objects' data value. The following would iterate through an array and return true if a data value in the array matches the input:
function checkDataValue(array, dataValue) {
let contains = false;
for (i = 0; i < array.length; i++) {
if (array[i]['data'] === dataValue) {
contains = true;
}
}
return contains;
}
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.