Help

Re: Completely stuck on filtering through and array

4834 1
cancel
Showing results for 
Search instead for 
Did you mean: 

Im trying to expand the report example, to make my own custom report.
However I’m completely stuck on filtering an array of records that contains a specific name.

I’ve tried a few different things (commented out below), but I don’t seem to get it to work.

I have a table called ‘report’ in that base there is a field called ‘names’ and I want to filter all away all the records that does not contain the value ‘Dan’ in my names field.

I hope someone has time to help an aspiring JS developer.

let tableReport = base.getTable('report')
let filter = 'Dan'

//loadReportRecords
let reportRecords = await tableReport.selectRecordsAsync();

//let filteredRecords = reportRecords.getRecord(reportRecords.recordIds).getCellValue("names")


//let filteredRecords = reportRecords.records.filter(record => record.getCellValue("names")===filter);

/*
let filteredRecords = reportRecords.records.filter(function(reportRecord){
    return reportRecord.getCellValue('names') === 'Dan';
}); 
*/
15 Replies 15

Hi @Stephen_Suen I hope one day I’ll get good at this.

It sounds to me your assumtions are right. However I still get the error saying:
TypeError: reportQuery.filter is not a function

@Kim_Trager1

reportQuery in your case is an Object - the Object has an array inside of it called records. That’s what you are wanting to filter. To access the records you queried, you need to pull them out of the query Object with reportQuery.records.

So you want:

let reportRecordsForPerson = reportQuery.records.filter(record => {
    ...
});

Ah yes, that was my mistake — I’ve updated the snippet @Kim_Trager1

Thank you @Jeremy_Oglesby for pointing out the obvious and make me understand…

I should have tried something like that considering my reportQuery output a bunch of objects.

Thank you @Stephen_Suen and thank you for introducing me to the some() method…

Kathy_Benson
6 - Interface Innovator
6 - Interface Innovator

I had a similar problem and I changed from getCellValue to getCellValueAsString which helped me. I’m not sure I fully understand your use case, but I wanted to offer this potential suggestion,.