I'm developing an dictionary app for FirefoxOS by Javascript . my json file look like this
[
{"id":"3784","word":"Ajar","type":"adv.","descr":" Slightly turned or opened; as, the door was standing ajar.","track":"a","track_2":"Aj","track_3":"Aja"},
{"id":"3785","word":"Ajar","type":"adv.","descr":" In a state of discord; out of harmony; as, he is ajar with the world.","track":"a","track_2":"Aj","track_3":"Aja"},{"id":"3786","word":"Ajog","type":"adv.","descr":" On the jog.","track":"a","track_2":"Aj","track_3":"Ajo"},
{"id":"3787","word":"Ajutage","type":"n.","descr":" A tube through which water is discharged; an efflux tube; as, the ajutage of a fountain.","track":"a","track_2":"Aj","track_3":"Aju"} ]
Now I want to query this json file where word = "aj" and get the all the matched results or id's of the matched results . How can I do that ?
TRY THIS UPDATED FIDDLE DEMO
var jsonArrr =[
{"id":"3784","word":"Ajar","type":"adv.","descr":" Slightly turned or opened; as, the door was standing ajar.","track":"a","track_2":"Aj","track_3":"Aja"},
{"id":"3785","word":"Ajar","type":"adv.","descr":" In a state of discord; out of harmony; as, he is ajar with the world.","track":"a","track_2":"Aj","track_3":"Aja"},{"id":"3786","word":"Ajog","type":"adv.","descr":" On the jog.","track":"a","track_2":"Aj","track_3":"Ajo"},
{"id":"3787","word":"Ajutage","type":"n.","descr":" A tube through which water is discharged; an efflux tube; as, the ajutage of a fountain.","track":"a","track_2":"Aj","track_3":"Aju"} ];
var matchMe = new RegExp('^' + 'aj', 'i');
var matches = [];
for (var i in jsonArrr) {
if (jsonArrr[i].word.search(matchMe) > -1 ) {
matches.push( {'id': i, 'word': jsonArrr[i].word} );
}
}
To see the result
for (var i in matches) {
console.log(matches[i].word);
//your code
}
Take console window in Inspect Element to see the result
var matchedIndexes = []
for (var i=0; i < array.length; i++) {
var obj = array[i];
if (obj.hasOwnProperty("word")) {
if(obj["word"].match(/aj/i)) {
console.log("matched");
matchedIndexes.push(i);
}
}
}
matchedIndexes
will contain all indexes in which object has word "aj" ( case insensitive ). array
in this code is your object of arrays.
Loop through the json array :
var result = null;
Objects.forEach(function(obj, i){
if(obj.word == 'aj'){
return result = obj;
}
});
console.log(result);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.