Javascript |对象、数组和函数
也许你可以帮助我。如何创建通过 id 返回对象值的全局对象和函数?
示例:
var chat = {
data : {
friends: {}
}
}
....
/*
JSON DATA RETURNED:
{"users": [{"friend_id":"62","name":"name","username":"admin","thumb":"images/avatar/thumb_7d41870512afee28d91.jpg","status":"HI4","isonline":""},{"friend_id":"66","name":"Another name","username":"regi","thumb":"images/avatar/thumb_d3fcc14e41c3a77aa712ae54.jpg","status":"Всем привет!","isonline":"avtbsl0a6dcelkq2bd578u1qt6"},{"friend_id":"2679","name":"My name","username":"Another","thumb":"images/avatar/thumb_41effb41eb1f969230.jpg","status":"","isonline":""}]}
*/
onSuccess: function(f){
chat.data.friends = {};
for(var i=0; i< f.users.length;i++){
chat.data.friends.push(f.users[i])
}
}
如何创建一个新函数(它将按friend_id返回值)?
get_data_by_id: function (what, friend_id) {
/*obj.what = getfrom_globalobject(chat.data.friends???)*/
}
使用示例:
var friend_name = get_data_by_id(name, 62);
var friend_username = get_data_by_id(username, 62);
var friend_avatar = get_data_by_id(thumb, 62);
may be you can help me. How can I create global object and function that return object values by id?
Example:
var chat = {
data : {
friends: {}
}
}
....
/*
JSON DATA RETURNED:
{"users": [{"friend_id":"62","name":"name","username":"admin","thumb":"images/avatar/thumb_7d41870512afee28d91.jpg","status":"HI4","isonline":""},{"friend_id":"66","name":"Another name","username":"regi","thumb":"images/avatar/thumb_d3fcc14e41c3a77aa712ae54.jpg","status":"Всем привет!","isonline":"avtbsl0a6dcelkq2bd578u1qt6"},{"friend_id":"2679","name":"My name","username":"Another","thumb":"images/avatar/thumb_41effb41eb1f969230.jpg","status":"","isonline":""}]}
*/
onSuccess: function(f){
chat.data.friends = {};
for(var i=0; i< f.users.length;i++){
chat.data.friends.push(f.users[i])
}
}
How can I create a new function (It will return values by friend_id)?
get_data_by_id: function (what, friend_id) {
/*obj.what = getfrom_globalobject(chat.data.friends???)*/
}
Example of use:
var friend_name = get_data_by_id(name, 62);
var friend_username = get_data_by_id(username, 62);
var friend_avatar = get_data_by_id(thumb, 62);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
尝试:
...但使用它:
...并设置映射:
Try:
... but use it like:
...and set up the mapping with:
您不能
.push()
到对象。对象是key =>; value
映射,因此您需要使用char.data.friends[somekey] = f.users[i];
如果您真的只想要一个带有数字键的列表,请制作 x5fastchat.data .friends 一个数组:
x5fastchat.data.friends = [];
但是,由于您希望能够通过
friend_id
访问元素,因此请执行以下操作:请注意
obj[what]
而不是原来的obj.what
:编写obj.what
时,what
的处理方式如下一个字符串,所以它等于obj['what']
- 但由于它是一个函数参数,所以您需要obj['what]
。You cannot
.push()
to an object. Objects arekey => value
mappings, so you need to usechar.data.friends[somekey] = f.users[i];
If you really just want a list with numeric keys, make x5fastchat.data.friends an array:
x5fastchat.data.friends = [];
However, since you want to be able to access the elements by
friend_id
, do the following:Note the
obj[what]
instead of your originalobj.what
: When writingobj.what
,what
is handled like a string, so it's equal toobj['what']
- but since it's a function argument you wantobj[what]
.看看下面的代码。您只需将其复制粘贴到 HTML 文件中并打开即可。单击“开始”,您应该会看到结果。如果我没有正确理解你的意思,请告诉我。 :
Take a look at the following code. You can simply copy paste it into an HTML file and open it. click "go" and you should see the result. let me know if I did not understand you correctly. :
我建议使用朋友对象,而不是通过 id 和名称获取它们。
I would recommend using the friend objects rather than getting them by id and name.