cancel
Showing results for 
Search instead for 
Did you mean: 

Using $.get on properties with forward slash

nickhumble
New Contributor II

Hi,

We have an array of the following structure:
{

    "/position/fte": {
      "humanReadable": "100",
      "value": 100
    },
    "/position/field_2354914": {
      "humanReadable": "ALQhtani & Partners",
      "value": "257532069"
    },
    "/position/field_2355402": {
      "humanReadable": "fhdfhjf",
      "value": "fhdfhjf"
    },
    "/position/field_7424424": {
      "humanReadable": "Indirect",
      "value": "257905227"
    },
    "/position/field_2355398": {
      "humanReadable": "Central Operations",
      "value": "257475809"
    },
    "/position/field_7424427": {
      "humanReadable": "Support",
      "value": "257905230"
    },
    "/position/job": {
      "humanReadable": "Information Technology \\ 2a",
      "value": 4807663
    },
    "/position/name": {
      "humanReadable": "P-1486729",
      "value": "P-1486729"
    },
    "/position/field_2355401": {
      "humanReadable": "Birmingham - Snow Hill",
      "value": "257529525"
    },
    "/position/department": {
      "humanReadable": "Legal Operations",
      "value": "257475756"
    },
    "/position/field_2355404": {
      "humanReadable": "Hybrid Worker",
      "value": "257781843"
    },
    "/position/field_7424426": {
      "humanReadable": "Support",
      "value": "258131652"
    },
    "/position/site": {
      "humanReadable": "England",
      "value": 2477909
    },
    "/position/field_7424425": {
      "humanReadable": "Support",
      "value": "258046626"
    },
    "/position/field_2355400": {
      "humanReadable": "Audit - Australia",
      "value": "257476016"
    },
    "/position/field_2355399": {
      "humanReadable": "Business Services",
      "value": "257475770"
    }
  }
 
Not all of the items in the array have the same properties, so I've been looking at using $.get to workaround this.  However using something like:
$.get("['/position/field_7424425'].value")
always returns null.
 
I can use $.hasPath("['/position/field_7424425'].value") to check if it exists and it correctly returns true/false so can use a ternary operator but would prefer to use .get if possible.
 
Can anyone advise?
1 ACCEPTED SOLUTION

Thanks @koryknick - this might just be the simplistic genius i was looking for!

View solution in original post

4 REPLIES 4

koryknick
Employee
Employee

@nickhumble - could you simply enable the "Null-safe access" option and always reference the values you're looking for?  If it doesn't exist, it will just be a null value.  This would bypass the need for the get() method, which as you have found out, can't be used under multiple levels of object nesting.

Thanks @koryknick - this might just be the simplistic genius i was looking for!

koryknick
Employee
Employee

If that doesn't work for you, then try this:

$.get('/position/field_2354910', {}).get('value')

Since the Object.get() method allows you to provide a default if the field doesn't exist, this example returns an empty object, so calling the next get() on the empty object will also return null.

Hope this helps!

Thanks - my problem was i couldn't get a non-null value using .get, no matter what i tried. I wondered whether the forward slashes were confusing it or something, but .hasPath worked ok so i'm a bit stumped.

Your earlier suggestion has worked well though - thank you.