Last active
August 29, 2015 14:09
-
-
Save shengoo/9e11b08cb7119e2828ce to your computer and use it in GitHub Desktop.
JavaScript array search/find
This file contains hidden or 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 jsonObj ={"list": [ | |
{"name":"my Name","id":12,"type":"car owner"}, | |
{"name":"my Name2","id":13,"type":"car owner2"}, | |
{"name":"my Name4","id":14,"type":"car owner3"}, | |
{"name":"my Name4","id":15,"type":"car owner5"} | |
]}; | |
var results = []; | |
var searchField = "name"; | |
var searchVal = "my Name"; | |
for (var i=0 ; i < jsonObj.list.length ; i++) | |
{ | |
if (jsonObj.list[i][searchField] == searchVal) { | |
results.push(jsonObj.list[i]); | |
} | |
} | |
alert(results[0]["id"]); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment