Forum Discussion

alex_panganiban's avatar
alex_panganiban
Contributor
5 years ago

Getting index value from an array value

Wondering if someone could answer this question for me. Given the below array, how would I get the index number of $arrayField.name ===“Alex”?

I can easily get the index of true and false by respectively doing this: $arrayField.indexOf(true) and $arrayField.indexOf(false), but I can’t figure out how to get the index of this particular name value. I know, it’s an array of mixed datatypes and that’s what’s complicating my issue, but I need to figure out how to resolve it.

I’ve even tried “$arrayField.findIndex(x => x.name == “Alex”).” This code works as a regular Javascript expression, but SnapLogic doesn’t seem to like it as it is rejecting the “x.name”. Hope someone can help.

$arrayField = [true, {‘name’: “Alex”, “age”: 60}, false]

2 Replies

  • bojanvelevski's avatar
    bojanvelevski
    Valued Contributor

    Hello @alex.panganiban.guild,

    You cannot access the ‘name’ field directly because as you said, you have a mixed data type array. You first need to access the object, and than the field that you need from that object. So , if you need to get the index number of an object, you can use this function:

    $arrayField.findIndex(x=>typeof x == “object”)

    And if you want the index number of the ‘name’ path of that object, you can use the following function:

    $arrayField[$arrayField.findIndex(x=>typeof x == “object”)].keys().findIndex(x=>x == ‘name’)

    Hope you’ll find this helpful,

    Regards,
    Bojan

  • Hi Bojan, thank you so much for your response and the solution you shared with me. It’s not exactly what I was hoping for, but it works great, so I’ll take it gratefully! Again, thanks so much for your help!

    Alex