Javascript |对象,数组和函数

时间:2011-12-27 11:59:17

标签: javascript arrays json object mootools

你可以帮助我。如何创建通过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);

4 个答案:

答案 0 :(得分:1)

你不能.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]

答案 1 :(得分:1)

尝试:

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];
}

答案 2 :(得分:0)

看看以下代码。您只需将其粘贴到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>

答案 3 :(得分:0)

我建议使用朋友对象,而不是通过id和name获取它们。

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);