Get the row index of a dataset/list matching criteria - Matches lookup
function but returns row index
I want to replicate the functionality of lookup(...)
except instead of returning a cell value, I want to return the index of the found row.
Wrap a forEach()
inside where()
to include the original index in the output.
Index Lookup
where(
forEach( // Expand the source into pairs of original index and content there
{path.to.source},
idx(),
it()
),
it()[1]['someColumn'] = 'SomeValue' // original content is the 2nd element of the pair
)[0][0] // Extract first match, then original index is the 1st element of the pair
Note that most applications that need the index need it to retrieve multiple parts of the found row. It is better to simply use where()
to return the entire first matching result. { I want to use this for the scrollIntoView
JS function which needs the row index }
If the source is sure to be a dataset, consider using selectStar()
to add a new column holding the original index instead of making pairs. Something like this:
Dataset Row Lookup
where(
where(
selectStar( // Expand the source to include an original index column
{path.to.source.dataset},
asMap('_original_idx', I),
idx()
),
it()['someColumn'] = 'SomeValue' // any ordinary condition
),
idx() < 1 // Extract first row
)