0

I keep trying different methods to check if this JSON contains "attributes." In this way I can determine if the given coordinates are outside of wetlands. If they are in wetlands, "attributes" will exist in the JSON. If they aren't in wetlands, 'attributes' won't be in the JSON.

When I run this function, I am only getting TRUE - when I type in coordinates that are in a wetland (try 43.088 instead, in the JSON url, which returns true).

However I want FALSE for the given url. For some reason when I do console.log("FALSE"), this doesn't appear or return in the console at all if hasOwnProperty('attributes') == false.

Am I missing something?

function(GetData) {

  fetch('https://www.fws.gov/wetlandsmapservice/rest/services/Wetlands/MapServer/0/query?where=&text=&objectIds=&time=&geometry=-88.305%2C43.060&geometryType=esriGeometryPoint&inSR=4326&spatialRel=esriSpatialRelWithin&relationParam=&outFields=WETLAND_TYPE&returnGeometry=false&returnTrueCurves=false&maxAllowableOffset=&geometryPrecision=&outSR=&returnIdsOnly=false&returnCountOnly=false&orderByFields=&groupByFieldsForStatistics=&outStatistics=&returnZ=false&returnM=false&gdbVersion=&returnDistinctValues=false&resultOffset=&resultRecordCount=&queryByDistance=&returnExtentsOnly=false&datumTransformation=&parameterValues=&rangeValues=&f=pjson&__ncforminfo=qCOZOO8Kyr4uogGcKvxkzzuK7gmavd4CxwTAkdbAsF2_aT4eeNbB0NpLwCYwiAJSf1ZHqY3CKVZ3osgMevhYGQrqRUQZej5oHaSmnSIaiZZb469Cexv-zqqmgYMuFJAAzrcRxvKXPBz9VnYPnMrM6kBNhO-cz6yK_w5T1mqNu_VXSbjBSihVf4_mlUBSVb9yf4C8scYXWm9Iak2Nfn1dtJACNUHLBHSElLvc1wxFMO2eUWNsD3qpCk3kAcRyYftuFU86n7THyk2IvkIUpxNmDHRxmmbgSYvPLMkl8t41Jzjp_bntkIyOWB0u8cQU2VsfASFUdznRkvrvYrQxgR8eyvsPq5oV_ZoPSksVCew6xev0K_TV2NU-kjojYpowMVXpZtCX9P-Q_7m8ywt2PyLPhEVgQB12ji1S7G5FRzIt6E0SDoXMY1vqQvPtedaNYbBCazXgs05L9DFKdtvrwmQVCeLmpBTduIhF9Sk4kozMnFX6GOANrZJMCI9AssN0DjrhlZkgDVw0l1flF44Zli927CXGTQ-oUpwsn7PPypVkN2iDJf-nz9XNbj82sv1c6B5s5UZVwiOp8VHJfZSDJ8BAYR4z_oONT2JwbVSKKlFKeN72f-Y6EejcB9wPKmn5kYjv7CKkRyIIv4F4cqVWxLK9x33uvEDMTvxX')
    .then(function(response) {
      return response.json();
    })
    .then(function(data) {
      appendData3(data);
    })

    .catch(function(err) {
      console.log('error: ' + err);
    });


  function appendData3(data) {
    for (let obj of data['features']) {

      if (obj.hasOwnProperty('attributes') == false) {
        console.log("FALSE");
      } else {
        console.log("TRUE");
      }
    }
  }

};
Mister Jojo
  • 20,093
  • 6
  • 21
  • 40
Jack
  • 53
  • 8
  • 1
    Are you sure that obj is an object and not an array? – Tasos Tsournos Jan 14 '22 at 22:33
  • 4
    `data['features']` is an empty array in your posted example so the `for...of` loop isn't running at all – pilchard Jan 14 '22 at 22:41
  • 1
    @pilchard ```data``` is supposed to be an object and OP is trying to access ```features``` key. – Tasos Tsournos Jan 14 '22 at 22:45
  • 2
    @AnastasiosTsournos `data` is an object, the OP is iterating over each object in the `data.features` array and checking each of those for an `attributes` key. In this example there are no objects in the `features` array so no checks are ever made. – pilchard Jan 14 '22 at 22:46
  • @pilchard oooohhhhhhh it all clicked when you said my for…of loop wasn’t even triggering which makes so much sense now. Solved it before the answer below but hey that works too! – Jack Jan 14 '22 at 22:59
  • Are you using the ArcGIS API for JavaScript? If so, there are easier ways :) – Bjorn Svensson Jan 14 '22 at 23:04

3 Answers3

1

The issue is that in the response data['features'] is empty. When iterating over an empty array, nothing within the for...of loop is executed.

const emptyArray = [];
for (const item of emptyArray) {
  // body is never executed...
}

If just checking the presence of an item within data['features'] is enough, you could use the length of the array.

function appendData3(data) {
  if (data.features.length > 0) {
    console.log("TRUE");
  } else {
    console.log("FALSE");
  }
}

To check if one of the elements has the property "attributes" you could use some():

function appendData3(data) {
  if (data.features.some(item => item.hasOwnProperty("attributes"))) {
    console.log("TRUE");
  } else {
    console.log("FALSE");
  }
}
3limin4t0r
  • 19,353
  • 2
  • 31
  • 52
0

If you're just trying to find out if a specific point is within one of the wetlands polygons, you could let the server to do the hard job and simplify your request. For example, ask for count.

See returnCountOnly at https://developers.arcgis.com/rest/services-reference/enterprise/query-feature-service-layer-.htm

Bjorn Svensson
  • 815
  • 8
  • 21
  • 1
    So, the reason I didn’t go this route is because I am not considering “Lake” or “Riverine” wetlands as wetlands, which would still have an attribute and return a count of 1. I filtered that out of my code for this question though. – Jack Jan 14 '22 at 23:49
  • 1
    You could use the "where" statement to filter out those unwanted wetlands features... – Bjorn Svensson Jan 14 '22 at 23:55
  • Ahh thanks for the tip. I just started using this query with almost no examples, so I don’t know the full capabilities of it yet – Jack Jan 15 '22 at 01:01
  • So just out of curiosity… is it possible to get the Labels instead of, or with the values in here? https://hazards.fema.gov/gis/nfhl/rest/services/public/NFHL/MapServer/28 …if so, any ideas on how to query that? – Jack Jan 15 '22 at 03:38
0

I tested your code and this is the problem. When the coordinates are outside the wetlands, the features array is empty, that means nothing happen in your for loop. So do this instead of checking directly inside of your for loop

function appendData3(data) {
    // Here we check if features is empty by checking it's length
    if (data['features'].length == 0) {
        console.log("FALSE")
    }

    for (let obj of data['features']) {
        console.log("TRUE");
    }

}

I also see that your for loop is only getting one object every time so instea of doing a for loop, just do it like this:

function appendData3(data) {
    var obj = data['features'][0]

    if(obj) {
      console.log('TRUE')
    } else {
      console.log('FALSE')
    }
}

As you can see, I did it even easier this time, just by getting the first object of features and checking if it exist.

Also, small tip: when you want to check if a condition is false, don't use == false, just put an exclamation mark at the beginning of the if statement. Like that:

if(!obj.hasOwnProperty('attributes')) { 
    // Code here will be executed if the condition is false
} else {
    // Code here will be executed if the condition is true
}

I hope this help you fixing your problem.

Have a nice day :)

Alex Dev
  • 1
  • 1