Apr 10, 2023 06:41 AM
let table = base.getTable("My table");
let query = await table.selectRecordsAsync({fields: [ 'Total' ]});
console.log('TEST 1 - uses filter() ');
let query = await table.selectRecordsAsync({fields: [ 'Total' ]});
for (let record of query.records.filter((each) => { 0 == each.getCellValue('Total') } )) {
console.log('Found a zero!');
}
console.log('TEST 2 - without filter() ');
for (let record of query.records) {
if ( 0 == record.getCellValue('Total') ) console.log('Found a zero!');
}
TEST 1 - uses filter()
TEST 2 - without filter()
Found a zero!
Found a zero!
Found a zero!
Apr 10, 2023 08:45 AM
Your anonymous function in your inline filter is not returning any records. This is because the function does not return a value, so all records fail the test.
Let's look at your filter
query.records.filter((each) => { 0 == each.getCellValue('Total') } ))
Notice that your filter function uses curly quotes for the code block of the function. When you use curly braces to indicate the beginning and end of the code block, you must use a return statement to return a value.
query.records.filter((each) => { return 0 == each.getCellValue('Total') } ))
If the function is a one-liner, you can remove the curly braces and the result of the one-liner is returned.
query.records.filter((each) => 0 == each.getCellValue('Total') ))
On the other hand, using an inline filter makes the code harder to read. Why not do the filtering first and then loop.
let query = await table.selectRecordsAsync({fields: [ 'Total' ]});
let recordsWhereTotalIsZero = query.records.filter(record => 0 == record.getCellValue('Total'))
for (let record of recordsWhereTotalIsZero ) {
console.log('Found a zero!');
}