BlogJavaScript. Two Dimensional ArraysTwo dimensional arrays are everywhere. For instance, all data coming from a database table would probably find its way to you as a two dimensional array. Here is an example of one: [ { "id":1, "first_name":"Tinna", "last_name":"Donne", } { "id":2, "first_name":"Joanne", "last_name":"Smith", } ]
Working with 2 dimensional arrays in JavaScript is not the nicest of experience. The standard Array functions do not provide anything out of the box to help with. Here are a few functions, attached to the Array prototype, which can make your life easier, when sorting through two dimensional arrays.
Here is an example how to use: // Get the person with ID=2 var person = people.selectOne(['id','=','2']);// Get the person with last name Smith var person = people.selectOne(['last_name','=','Smith']);// Get all the people whose first name is Joanne var joannes = people.selectWhere(['last_name','=','Smith']);
|