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);
share|improve this question

58% accept rate
1  
I suggest you learn how to program instead of copypasting bits of code... even the first snippet just screams "copypasted from somewhere without understanding". – Esailija Dec 27 '11 at 12:05
feedback

4 Answers

up vote 1 down vote accepted

Try:

get_data_by_id: function (what, friend_id) {
   return chat.data.friends[friend_id][what];
}

... but use it like:

var friend_name     = get_data_by_id('name', 62);

...and set up the mapping with:

for(var i=0; i< f.users.length;i++){
  chat.data.friends[f.users[i].friend_id] = f.users[i];
}
share|improve this answer
Thanks! It works. – user889349 Dec 27 '11 at 12:18
@user889349 You're welcome :) – sje397 Dec 27 '11 at 22:45
feedback

I would recommend using the friend objects rather than getting them by id and 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);
share|improve this answer
feedback

You cannot .push() to an object. Objects are key => value mappings, so you need to use char.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:

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

Note the obj[what] instead of your original obj.what: When writing obj.what, what is handled like a string, so it's equal to obj['what'] - but since it's a function argument you want obj[what].

share|improve this answer
Nice...What about: getfrom_globalobject? Is this a function? – user889349 Dec 27 '11 at 12:12
1  
Or it will be: obj[what] = chat.data.friends[friend_id][what]? – user889349 Dec 27 '11 at 12:13
feedback

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. :

<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>
share|improve this answer
feedback

Your Answer

 
or
required, but never shown
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.