Find All using nested arrays
Hi. I'm working with an interesting problem. I have arrays that are nested and need to search the main arrays based on values in the nested ones. Here's an example from the Xano Documentation
[
{
"id": 4,
"created_at": 1632268901955,
"make": "honda",
"model": "civic",
"year": 2002,
"maintenance": [
{
"year": 2004,
"description": "New bumper"
},
{
"year": 2009,
"description": "Radiator"
}
]
},
{
"id": 1,
"created_at": 1632268899571,
"make": "toyota",
"model": "corolla",
"year": 1998,
"maintenance": [
{
"year": 2015,
"description": "New engine"
},
{
"year": 2008,
"description": "Transmission"
}
]
},
{
"id": 2,
"created_at": 1632268900442,
"make": "jeep",
"model": "wrangler",
"year": 2007,
"maintenance": [
{
"year": 2016,
"description": "Suspension"
}
]
},
{
"id": 3,
"created_at": 1632268901252,
"make": "ford",
"model": "4 runner",
"year": 2004,
"maintenance": [
{
"year": 2011,
"description": "Radiator"
},
{
"year": 2013,
"description": "Passenger door"
}
]
}
]
If I want to search for records that have maintenance of Radiator, I should get back 2 records.
Using dot notation, it's as though the nested array is not visible, or I'm using the wrong technique to filter the records.
Help!
Comments
-
Using dot notation to manipulate/search on arrays/objects is not a good idea. Better use array/object functions and array/objects filters.
- https://docs.xano.com/working-with-data/functions/data-manipulation/arrays
- https://docs.xano.com/working-with-data/functions/data-manipulation/objects
- https://docs.xano.com/working-with-data/data-type-filters/array-filter
- https://docs.xano.com/working-with-data/data-type-filters/manipulation-filters
Maybe @Ray Deck can make a video to show how to do it in your case.
-
Hi,
have you tried array:find all elements? It seems like a solution for this problem. You can specify the path to check and filter by using $this.
So what you can do is to create empty array, loop through the main array with cars and then use array:find all elements on the nested array, if it's not empty then you are adding the record to your empty array. As a result you will receive only cars that meet the filter conditions.
Not sure if you are getting the array from Xano or some external API, but this solution is not perfect for frequent and extensive searches. So if it is from your database then I would think about updating it a bit so and maybe use search index. Although I would need more information to advice some upgrades. -
Thanks @Pawel Magdanski.
This is the filter I'm trying and it doesn't seem to work. Has it worked for you?
Find all and then use $this.maintenance.description looking for Radiator. It returns an error unable to locate var.
-
Can you show your function stack? It seems that some variable is missing.
-
Hi @Pawel Magdanski. Check out my stack below..
Using the same JSON I had shared is returning a blank result.
-
Try checking that maintenance.description exists first. Here's how:
Make your condition for the "find all" have two parts connected with "AND":
1) $this | has "maintenance.description"
2) $this.maintenance.description = "whatever I am looking for"
The "has" filter checks that the path in question exists on your object. If that fails, it won't even run the second test, which means you won't get the "unable to locate var" error. -
Hi @Ray Deck
Thanks for chipping in. No longer getting the unable to locate var error. Now it's just an empty result…
-
looking back at the original data structure I see the nested array that started you down this road. There is maintenance, but it does not contain description. It contains a list of objects that contain descriptions.
So this requires a little more verbosity in the no code stack. I've done this a few times on the State Change youtube channel, and I think this video might help give some insight on how you can iterate over nested arrays:
Categories
- All Categories
- 53 ? Announcements
- 47 ? Releases
- 37 ? Welcome
- 983 ? Help! I'm a Noob
- 125 ? No-Code Front-Ends
- 633 ? Working with APIs
- 439 ? Transforming data
- 126 ? Connect Xano to ...
- 50 ?? Find an Expert
- 348 ❓Other questions
- 35 ? Security
- 22 ✂️ Snippets
- 19 ? Showcase
- 7 ?️ Xano Chatter
- 62 ? Video Tutorials
- 171 ? Request a feature
- 229 ? Report a Bug
- 19 ? Templates & Extensions
- 7 ? Feedback