jQuery: Identify a value of declared variable is the same as the value within a list of keys in an array -
i have set of array in json
followed:
{"status":200,"success":true,"result": [{"id":"0","name":"user1","type":"mf","message":"bonjour user1"}, {"id":"1","name":"user2","type":"ff","message":"hello user2"}, {"id":"2","name":"user3","type":"mm","message":"konnichiwa user3"}, {"id":"3","name":"user4","type":"mf","message":"ni hao user4"}, {"id":"4","name":"user5","type":"ff","message":"high 5! user5"}]}
i ask how identify if value of declared variable (eg. "getname" value of "user1") same value within list of "name" keys in array?
below attempt , might find full code here:
var apiurl = "https://sheetsu.com/apis/f924526c"; $.getjson(apiurl,function(json){ console.log(json.stringify(json)); var getname = $.each(getname, function(content, json){ console.log(json.name); console.log(getname[content].id); }); var identifiedid = $.each(getname, function(){ console.log(this.name); console.log(this.id); }) });
i not sure if understood code, in order loop trough json
, find key value want use:
$.each(json.result, function() { //here can store id. $.each(this, function(key, value) { console.log(key + ' ' + value); //test if key = username want , store value }); });
you can 1 loop since know key check
$.each(json.result, function() { if (this.name == "name for"){ idyouneed = this.id; } }
Comments
Post a Comment