Javascript |对象、数组和函数
Javascript | Objects, Arrays and functions
也许你能帮助我。我如何创建全局对象和函数,返回对象值的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);
尝试:
get_data_by_id: function (what, friend_id) {
return chat.data.friends[friend_id][what];
}
…但是像这样使用:
var friend_name = get_data_by_id('name', 62);
…并使用:
设置映射for(var i=0; i< f.users.length;i++){
chat.data.friends[f.users[i].friend_id] = f.users[i];
}
您不能.push()
到对象。对象是key => value
映射,所以需要使用char.data.friends[somekey] = f.users[i];
如果你只想要一个带有数字键的列表,让x5fastchat.data.friends成为一个数组:x5fastchat.data.friends = [];
但是,由于您希望能够通过friend_id
访问元素,请执行以下操作:
onSuccess: function(f){
x5fastchat.data.friends = {};
for(var i=0; i< f.users.length;i++){
chat.data.friends[f.users[i].friend_id] = f.users[i]
}
}
get_data_by_id: function (what, friend_id) {
obj[what] = chat.data.friends[friend_id][what];
}
注意obj[what]
而不是原来的obj.what
:当编写obj.what
时,what
被处理得像一个字符串,所以它等于obj['what']
-但由于它是一个函数参数,您想要obj[what]
。
看看下面的代码。您可以简单地将其复制粘贴到HTML文件中并打开它。点击"go",你应该会看到结果。如果我没有理解正确,请告诉我。:
<script>
myObj = { "field1" : { "key1a" : "value1a" }, "field2" : "value2" }
function go()
{
findField(myObj, ["field2"])
findField(myObj, ["field1","key1a"])
}
function findField( obj, fields)
{
var myVal = obj;
for ( var i in fields )
{
myVal = myVal[fields[i]]
}
alert("your value is [" + myVal + "]");
}
</script>
<button onclick="go()">Go</button>
我建议使用好友对象,而不是通过id和名称获取它们。
DATA = {"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":""}]}
// simple data store definition
Store = {items:{}};
NewStore = function(items){
var store = Object.create(Store);
store.items = items || {};
return store
};
Store.put = function(id, item){this.items[id] = item;};
Store.get = function(id){ return this.items[id]; };
Store.remove = function(id){ delete this.items[id]; };
Store.clear = function(){ this.items = {}; };
// example
var chat = {
data : {
friends : NewStore()
}
}
// after data loaded
chat.data.friends.clear();
for( var i = 0; i < DATA.users.length; i += 1 ){
var user = DATA.users[i];
chat.data.friends.put( user.friend_id, user );
}
getFriend = function(id){ return chat.data.friends.get( id ); }
var friend = getFriend(66);
console.log(friend.name);
console.log(friend.username);
console.log(friend.thumb);
相关文章:
- 使用jQuery浏览函数数组
- coffescript/javascript函数数组的行为真的很奇怪
- 返回多个变量的 JavaScript 函数:数组与对象.最佳实践是什么
- 如何在 JavaScript 中动态地编写可迭代的函数数组
- JavaScript 自包含函数数组访问自我索引
- 如何在javascript中调用函数数组的索引号
- 从外部函数(数组)了解返回函数(x)
- Javascript函数数组
- 引用函数数组中的另一个函数
- 文本框搜索/Javascript函数数组**不对应**
- 返回一个函数数组
- selenium-webdriver 将函数数组作为参数传递给 executeScript
- 70%的时间运行JavaScript函数,从函数数组中随机选择
- 如何循环函数数组并在单击时一次执行一个
- 如何在 Javascript 命名空间脚本中创建函数数组
- 创建函数数组
- 在 Jscript 中为 Q.all() 构建一个动态函数数组
- 打印函数数组的函数名称
- 循环遍历函数数组 - 对象不支持此属性或方法 (IE8)
- 将函数数组传递给另一个函数