在 jQuery 中动态添加元素后无法获取字典的长度
Can't get the length of a dict after adding element dynamically in jQuery
首先,我使用 ajax 通过添加到我在 ajax.
之前声明的 dict 变量来接受 JSON 数据
我想获取dict的长度,但是这里输出:
the output
并且 dict 输出旁边的消息显示 "Value below was evaluated just now":
the message
这是我的代码:
<script>
$(function () {
var table_id = $('#table_id').text();
var list_length = $.words_list(table_id);
});
//function
jQuery.extend({
'words_list': function (id) {
var send_list = {};
$.ajax({
type: 'GET',
url: '/get_wordslist/' + id,
success: function (returnData) {
$.each(returnData, function (item, value) {
send_list[item] = value;
});
}
});
console.log(send_list);
console.log('length:' + Object.keys(send_list).length);
return 1;//just for returning
}
});
</script>
根据消息,我想我需要使用另一个变量来记录长度,像这样:
<script>
$(function () {
var table_id = $('#table_id').text();
var list_length = $.words_list(table_id);
});
jQuery.extend({
'words_list': function (id) {
var send_list = {};
var list_length = 0;// add a variable
$.ajax({
type: 'GET',
url: '/get_wordslist/' + id,
success: function (returnData) {
$.each(returnData, function (item, value) {
send_list[item] = value;
list_length++;//record increase
});
}
});
console.log(send_list);
console.log('length:' + list_length);//show
return 1;//just for returning
}
});
</script>
但是输出是一样的。我比较了这样一个简单的代码:
var dict = {
"Jeremy": 20,
"Jimmy": 30
};
console.log(dict);
console.log(Object.keys(dict).length);
这是输出:
example output
这是我的预期结果。根据消息,我认为动态添加元素到dict是问题的原因。但是我尝试用一个变量来记录长度,也失败了。现在我不知道如何解决这个问题。请帮帮我,告诉我原因,先谢谢了。
因为 ajax 是异步的,所以在你 console.log('length:' + list_length);
的时候,成功函数还没有 运行。要获得预期结果,您需要登录成功功能。像这样:
var send_list = {};
var list_length = 0;// add a variable
$.ajax({
type: 'GET',
url: '/get_wordslist/' + id,
success: function (returnData) {
$.each(returnData, function (item, value) {
send_list[item] = value;
list_length++;//record increase
});
// Log here
console.log(send_list);
console.log('length:' + list_length);
}
});
首先,我使用 ajax 通过添加到我在 ajax.
之前声明的 dict 变量来接受 JSON 数据我想获取dict的长度,但是这里输出:
the output
并且 dict 输出旁边的消息显示 "Value below was evaluated just now":
the message
这是我的代码:
<script>
$(function () {
var table_id = $('#table_id').text();
var list_length = $.words_list(table_id);
});
//function
jQuery.extend({
'words_list': function (id) {
var send_list = {};
$.ajax({
type: 'GET',
url: '/get_wordslist/' + id,
success: function (returnData) {
$.each(returnData, function (item, value) {
send_list[item] = value;
});
}
});
console.log(send_list);
console.log('length:' + Object.keys(send_list).length);
return 1;//just for returning
}
});
</script>
根据消息,我想我需要使用另一个变量来记录长度,像这样:
<script>
$(function () {
var table_id = $('#table_id').text();
var list_length = $.words_list(table_id);
});
jQuery.extend({
'words_list': function (id) {
var send_list = {};
var list_length = 0;// add a variable
$.ajax({
type: 'GET',
url: '/get_wordslist/' + id,
success: function (returnData) {
$.each(returnData, function (item, value) {
send_list[item] = value;
list_length++;//record increase
});
}
});
console.log(send_list);
console.log('length:' + list_length);//show
return 1;//just for returning
}
});
</script>
但是输出是一样的。我比较了这样一个简单的代码:
var dict = {
"Jeremy": 20,
"Jimmy": 30
};
console.log(dict);
console.log(Object.keys(dict).length);
这是输出:
example output
这是我的预期结果。根据消息,我认为动态添加元素到dict是问题的原因。但是我尝试用一个变量来记录长度,也失败了。现在我不知道如何解决这个问题。请帮帮我,告诉我原因,先谢谢了。
因为 ajax 是异步的,所以在你 console.log('length:' + list_length);
的时候,成功函数还没有 运行。要获得预期结果,您需要登录成功功能。像这样:
var send_list = {};
var list_length = 0;// add a variable
$.ajax({
type: 'GET',
url: '/get_wordslist/' + id,
success: function (returnData) {
$.each(returnData, function (item, value) {
send_list[item] = value;
list_length++;//record increase
});
// Log here
console.log(send_list);
console.log('length:' + list_length);
}
});