How to filter an array of objects from a dataset type field

So I have a row in the dataset (Collection) consist of a field type “place” that has this array of objects:

[
    {"location": "ABC1", "maxCapacity": 8}
    {"location": "ABC2", "maxCapacity": 8}
    {"location": "ABC3", "maxCapacity": 8}
    {"location": "ABC4", "maxCapacity": 3}
]

And I want to display this row when I select a “maxCapacity” of more or equal to 3. Else, I do not want to display it but it does not filter correctly by iterating through the objects in the array.

let newFilter = wixData.filter();
const first = newFilter. ge("place.maxCapacity", Number($w('#dropdown1).value));
const second = newFilter.contains("place.location", $w("#dropdown2).value));
allFilter = first.and(second);
$w("#dataset1").setFilter(allFilter).then(()=>{//.....});

Hi, Please use this code to filter the array.

const arrayOfItems= [
		{"location": "ABC1", "maxCapacity": 8},
    {"location": "ABC2", "maxCapacity": 8},
    {"location": "ABC3", "maxCapacity": 8},
    {"location": "ABC4", "maxCapacity": 3}
];

function myFunction() {
console.log("hello");
var newArray = arrayOfItems.filter(function (el)
{ return el.maxCapacity >= 3;
})
console.log(newArray);
}

You can replace 3 with the dropdown value. I hope this will help you :slight_smile:

So do I need do this before pushing it into setFilter? And also my array is in a dataset

Hi, you syntax to use filter is wrong. If you’re using filter on dataset then I didn’t see dataset element in your code.

Use wixData.query() method to find all the result or use filter conditions here. Please refer to velo WixData API reference.

If you choose to get all results then you can store them in an array and filter to get desired result.

Answer as you ask, use the dataset array as arrayOfItem as in last reply. If you use this method, then no need to use setfilter()

Sorry if I’m not clear but I have a dataset consists of a column with field type of arrays of objects as stated above, I was just wondering on how you can filter through the arrays of objects with respect to the row in a dataset, because my dataset is connected to a repeater.

Let’s say if a row has this array of object under the fieldType called “place”:

[
    {"location":"ABC1","maxCapacity":8}
    {"location":"ABC2","maxCapacity":8}
    {"location":"ABC3","maxCapacity":8}
    {"location":"ABC4","maxCapacity":3}
]

When my dropdown selection is 4, it shouldn’t show this row because one of the location has a maxCapacity of less than 4, the issue I’m having is how to iterate through the arrays of objects in that column?

Or is there a better way to setup my collection to suit my needs?

I’m using this setFilter - Velo API Reference - Wix.com

@samuellmkit Please use this code. Here you’re querying your collection to get all the items.

import wixData from 'wix-data';

let finalarray;
wixData.query("Your_Collection_Name")
  .find()
  .then( (results) => {
    if(results.items.length > 0) {
    for( var i=0; i<results.items.length; i++){
      let arrayOfItems= results.items[0].place; //see item below
      function myFunction() {
console.log("hello");
var newArray = arrayOfItems.filter(function (el){ return el.maxCapacity >= $w('#dropdown').value;})
console.log(newArray);
}
finalarray.push(newArray);
}
    
    } else {
      console.log("no item");
    }
  } )
 .catch( (err) => {
    let errorMsg = err;
    console.log(errorMsg);
  } );

I hope this helps. Please let me know this not works. Also send the screenshots of database, screen & console.

I’ve got a very similar set up except my array is:

{label: “bob”,value: “email@email.com”},{label: “john”,value: [“john@email.com”}]

for some reason my filter doesn’t like comparing the value to an email from a another variable but will return positive when the comparison is hardcoded “john@email.com”.

let array = emailList . setFilter ( function ( el ){ return el.value == selectEmail }) //does not work

let array = emailList . setFilter ( function ( el ){ return el.value == "john@email.com }) //works

what am I missing?

May be you have some in assigning the right value. Means check once the variable doesn’t change in the code.

Also, please check that selectEmail is not an object & also try to covert that to string.

Also, please close the inverted commas in the working code to avoid any issues.

For further help, please share the whole code.

I tried this and I get a list of email addresses in the console. the 8th pair is identical to each other, yet still an empty array is returned.

newArray = await memList . filter ( function ( el ){ console . log ( el.value ); console . log ( selectEmail ); return el.value == selectEmail })

Here is the code (removed all of the extraneous parts)

export async function scheduleButton_click_1 ( event ) {
let newArray = [];
const data = $w ( “#adminSched” ). data ;
let clickedItemData = data . filter ( item => item._id === event.context.itemId )

**let**  itemData  =  clickedItemData [ 0 ]; 
**let**  selectEmail  =  itemData.reservedBy ; 
    

newArray  =  **await**  memList . filter ( **function**  ( el ){ console . log ( el.value ); 	     console . log ( selectEmail ); **return**  el.value  ==  selectEmail }) 

console . log ( newArray ); 

}    

//note: memList is an array with 10 or so label/value pairs as I similar to what I have listed above.

Inside the function, use a for loop to compare the value with email.

Because you’re comparing a single value with an array that’s why you didn’t get the result you want.

@wix-expert-velo-cert I’ll try that but why would I get the result I wanted when using a hardcoded value? Doesn’t the filter() function iterate through the array? It is supposed to “gather” all of the items that meet the criteria and put them in the newArray variable.

What’s the point of a filter function if I have to manually compare using a for loop?

Are you sure about the variable value
let selectEmail = itemData.reservedBy;

I request you to use the console.log once to check this value.

Also inside the function too. In this way we are assured that value & email are same or not.

@wix-expert-velo-cert Yes. when I tried this:

newArray = await memList . filter ( function ( el ){ console . log ( el.value ); console . log ( selectEmail ); return el.value == selectEmail })

console looked like this (fake emails):

1email@test.com
selectEmail @email.com
2email@test.com
selectEmail @email.com
3 email@test.com
selectEmail @email.com
4 email@test.com
selectEmail @email.com
selectEmail @email.com
selectEmail @email.com
…etc

there are only 10 or objects in the memList array. The 8th one is a match for the repeater item i am clicking on to trigger the filter.

I really do appreciate your effort here. I really want to know what I’m overlooking even though I’m going to feel very stupid when I find it. :grinning:

@wix-expert-velo-cert Call off the dogs!
I think I found my INCREDIBLY STUPID mistake! I’m going back to test now. Stay tuned. I might be too embarrassed to return so thank you for all of your effort here! :disappointed:

@wix-expert-velo-cert I owe you a drink or at least 30 min of your life back. I found an incredibly stupid spelling error in my data and therefore the comparison I was testing was never going to work.

The filter function works as expected! I’m incredibly grateful for your time and energy. Some good that came from this is I am very well versed in filter function now but I’m embarrassed by the time I’ve wasted…both yours and mine.

Have a wonderful rest of your life! :grinning:

Great to hear that it’s resolved finally. Yes, this happens in coding. Best part is use console to check what’s happening.

Have fun with Wix Coding, have a great day ahead!