javascript - Output value from Json Array -


i'm getting output: [{"ref":"contact.html","score":0.7071067811865475}]

 $.getjson( "index.json", function(content) {           idx = lunr.index.load(content);         var results = idx.search(variabletosearch);         var final = json.stringify(results);         console.log(final); }); 

how can print value of ref? when console.log(final[0].ref); undefined.

edit:

json.stringify returns string, not object. in case 'final' string contains same data in object 'results'

to access data object, can use result[0].ref, or if want use 'final' (although don't need to), can this:

final = json.parse(final) console.log(final[0].ref) 

if it's 1 object within array, other answers enough, since arrays used store multiple objects, can follows:

var arr = [     {"ref": "object1", "score": "1"},     {"ref": "object2", "score": "2"} ]  arr.map(function(object) {     console.log(object.ref) }) 

jsfiddle using alert instead of console.log()

you can use loop, cleaner.


Comments

Popular posts from this blog

unity3d - Rotate an object to face an opposite direction -

angular - Is it possible to get native element for formControl? -

javascript - Why jQuery Select box change event is now working? -