Filter Response
I have the following response from my database as part of a larger dataset. How would one filter this to only include the offers where type (which is an enum) === "product"?
I have gone round and round and can't seem to find the best way to create a variable with only the product offers.
[[{"id":1,"created_at":1678757095682,"name":"Monthly Legacy Card","description":"","type":"action","product_id":0,"duration":0,"_product":[]}],[{"id":3,"created_at":1678757295097,"name":"10% Discount","description":"","type":"discount","product_id":0,"duration":0,"_product":[]}],[{"id":2,"created_at":1678757212295,"name":"Digital Download ","description":"","type":"coupon","product_id":0,"duration":0,"_product":[]}],[{"id":4,"created_at":1678773932876,"name":"One Month Humanity Magazine","description":"","type":"product","product_id":2,"duration":1,"_product":[{"id":2,"created_at":1678757891298,"name":"Humanity Magazine","price":9.99,"code":"MAG-HUMANITY","category_id":2,"main_image":{"path":"/vault/T8h5_tNd/9NekvK2JWrcSmewJRhtelZOyX6w/r_L2VQ../IHL_Mockup.png","name":"IHL_Mockup.png","type":"image","size":345168,"mime":"image/png","meta":{"width":581,"height":395},"url":"https://x8ki-letl-twmt.n7.xano.io/vault/T8h5_tNd/9NekvK2JWrcSmewJRhtelZOyX6w/r_L2VQ../IHL_Mockup.png"}}]}]]
Comments
-
Maybe this is a better view. I want offers to only be the offers from the list that are of type product.
-
I'm not sure I understand your question. Do you want to filter your data in your database query or in the resulting object that your database query returns ?
Also, is your multidimensional unnamed array intentional ? (starts with [[ and ends with ]])… as in can a second level array may have multiple objects in them? This might complexify filtering the data from the object returned. If your child array will always return one line, perhaps look at your Query output to make sure it returns a single line instead of a list (so it will return { } instead of [ { } ] with one single object in each array). Otherwise, discard this recommendation. -
In all cases… for filtering the object result of your array.
If your child arrays will always have only a single element :
You can do something like so :
query_response : The input you gave in your first postproduct_type_only : The resulting output.
Result
If your child arrays may have multiple objectYou'll have to do a for each then do the array search for each array… then push those to your resulting array
-
I want to filter the resulting object from the database. I pull all of the offers that go with the record, and then I need to process each of the offer types in a different way.
-
Hi @pgiesin — I don't see an offers field in your screenshot. Maybe I'm missing something, can you please clarify? Typically filtering a query can be done through the By Custom Query section of the Query all records function. It allows you set WHERE statements to filter the data.
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