Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

easiest way to search a javascript object list with jQuery?

What is the easiest way to search a javascript object list with jQuery?

For example, I have the following js config block defined:

var ProgramExclusiveSections =
{
   "Rows":
   [
      { 'ProgramId': '3', 'RowId': 'trSpecialHeader'},
      { 'ProgramId': '3', 'RowId': 'trSpecialRow1' },
      { 'ProgramId': '3', 'RowId': 'trSpecialRow2' },
      { 'ProgramId': '1', 'RowId': 'trOtherInfo' }
   ]
} 

The user has selected Program ID = 3 so I want to get only the "rows" that I have configured in this js config object for Program ID = 3. This will get me the javascript object list:

var rows = ProgramExclusiveSections.Rows

but then I need to filter this down to only where RowId = 3. What's the easiest way for me to do this with jquery?

like image 263
Manikandan Thangaraj Avatar asked Feb 23 '23 09:02

Manikandan Thangaraj


1 Answers

$.grep()

var matches = $.grep(rows, function (elt)
{
    return elt.ProgramId === '3';
});
like image 118
Matt Ball Avatar answered May 01 '23 18:05

Matt Ball