Iterating through Json in Jquery
Hi everyone and thanks in advance for helping me out. first of all, apologies if I use the wrong phrase here. I'm not so worried about syntax here, just getting this to work. Now, the issue: I have the django serializer outputting the following JSON:
[
{
"pk": 11262,
"model": "dict.words",
"fields": {
"lang": "KO",
"incorrect": null,
"sug_translation": [
3215
],
"word": "\uc0dd\uac01\ud558\ub2e4",
"definition": [],
"pos": "VE",
"phrase": [],
"translation": [
{
"pk": 1,
"model": "dict.words",
"fields": {
"word": "comprender"
}
},
{
"pk": 6028,
"model": "dict.words",
"fields": {
"word": "entender"
}
}
],
"incomplete": null
}
}
]
What I'd like to do is get to fields.translation.fields.words and thus the Jquery for the Autocomplete is
$(function() {
$( "#query_form" ).autocomplete({
minLength: 2,
source: 'http://127.0.0.1:8000/json_results/',
focus: function( event, ui ) {
$( "#query_form" ).val( ui.item.word );
return false;
},
select: function( event, ui ) {
$.get ('http://127.0.0.1:8000/json_detail/',
{
item: ui.item.pk
},
function(data) {
$('#query_result').prepend(data);
});
$( "#query_form" ).val( ui.item.word );
return false;
}
})
.data( "autocomplete" )._renderItem = function( ul, item ) {
var tran = $.each(item.fields.translation, function(i){item.fields.translation[i].fiel开发者_运维问答ds.word})
return $( "<li></li>" )
.data( "item.autocomplete", item )
.append("<a>" + item.fields.word + tran + "</a>")
.appendTo( ul );
};
});
I'm a total noob to jquery and javascript in general, so pardon any glaring formatting errors. Anyways, The problem here is, while this does actually make the request, and the autocomplete functions, the $.each(item.fields.translation, function(i) item.fields.translation[i].fields.word}) returns [object, Object] in the autocomplete list. If I have it output via alert() it returns the correct values. If I just use item.fields.translation[0].fields.word in the .append line, it outputs the value. But for some reason, when i ask it to do what I want it to do, I get [object Object] So, anyone have any idea what I'm doing wrong? Thanks a ton in advance!
You're setting your variable tran
equal to the $.each()
function, which returns jQuery
. That's why it ends up being an object.
I'm not clear on what exactly you're trying to do. You're looping over the items in the item.fields.translation
array, but ultimately doing an append as if this loop should just return a single string. But item.fields.translation
has two items in its array, so... You could build an array like this:
var tran = [];
$.each(item.fields.translation, function(i){
tran.push(item.fields.translation[i].fields.word);
});
//tran now equals ['comprender','entender']
Not sure if that helps. If you clarify what you are expecting tran
to be, then I could help further.
And a side note: you can pass the value of the item in the current iteration--rather than just its index/key--to the function inside $.each()
. For example:
$.each(item.fields.translation, function(i,v){
//the following is the same as tran.push(item.fields.translation[i].fields.word);
tran.push(v.fields.word);
});
精彩评论