var user = {};
now I want to create a setUsers()
method that takes a key/value pair object and initializes the user
variable.
setUsers = function(data) {
// loop and init user
}
where data is like:
234: "john", 23421: "smith", ....
This question is related to
javascript
loops
for-loop
iteration
key-value
for (var key in data) {
alert("User " + data[key] + " is #" + key); // "User john is #234"
}
Something like this:
setUsers = function (data) {
for (k in data) {
user[k] = data[k];
}
}
Source: Stackoverflow.com