Last active
August 29, 2015 14:08
-
-
Save tombowers/08ceabcbf4a3154d5674 to your computer and use it in GitHub Desktop.
How to search for an object in a Javascript array, and fast!
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var food = { | |
fruit: [ | |
{ name: 'banana', value: 'joke fruit', type: 'fruit' }, | |
{ name: 'apple', value: 'newtonian legend', type: 'fruit' } | |
], | |
vegetables: [ | |
{ name: 'cabbage', value: 'filth', type: 'vegetable' }, | |
{ name: 'broccoli', value: 'green evil', type: 'vegetable' } | |
] | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var food = [ | |
{ name: 'banana', value: 'joke fruit', type: 'fruit' }, | |
{ name: 'apple', value: 'newtonian legend', type: 'fruit' }, | |
{ name: 'cabbage', value: 'filth', type: 'vegetable' }, | |
{ name: 'broccoli', value: 'green evil', type: 'vegetable' } | |
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function getFood(name, context) { | |
var obj, i, il; | |
for (i = 0, il = food[context].length; i < il; i++) { | |
if (food[context][i].name == name) { | |
obj = food[context][i]; | |
break; | |
} | |
} | |
return obj; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function getFood(name, context) { | |
var obj, i, il; | |
if (context) { | |
for (i = 0, il = food[context].length; i < il; i++) { | |
if(food[context][i].name == name) { | |
obj = food[context][i]; | |
break; | |
} | |
} | |
} else { | |
for (i = 0, il = food.length; i < il; i++) { | |
for (j = 0, jl = food[i].length; j < jl; j++) { | |
if(food[i][j].name == name) { | |
obj = food[i][i]; | |
break; | |
} | |
} | |
if (obj) { | |
break; | |
} | |
} | |
} | |
return obj; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
broccoliObject = getFood('broccoli'); | |
// Or | |
broccoliObject = getFood('broccoli', 'vegetable'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function getFood(name) { | |
var obj, i, il; | |
for (i = 0, il = food.length; i < il; i++) { | |
if (food[i].name == name) { | |
obj = food[i]; | |
break; | |
} | |
} | |
return obj; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var broccoliObject = food[foodIndex.broccoli]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var food = [ | |
{ key: 21, name: 'banana', value: 'joke fruit', type: 'fruit' }, | |
{ key: 52, name: 'apple', value: 'newtonian legend', type: 'fruit' }, | |
{ key: 2, name: 'cabbage', value: 'filth', type: 'vegetable' }, | |
{ key: 15, name: 'broccoli', value: 'green evil', type: 'vegetable' } | |
]; | |
var foodIndex = { | |
banana: 0, | |
apple: 1, | |
cabbage: 2, | |
broccoli: 3 | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment