Reputation: 2764
How can I get the corresponding object from data where the value (for example 888
) of a key (for example id
) is equal to the value of a looping variable (i.e. id[i] == 888
)?
My data looks like:
{
"players": {
"player6": {
"id": "777",
"name": "Barak Obama",
"trck": "obama",
"img": "dev/obama.jpg",
"img2x": "dev/obama_2x.jpg"
},
"player23": {
"id": "888",
"name": "George Bush",
"trck": "bush",
"img": "dev/bush.jpg",
"img2x": "dev/bush_2x.jpg"
},
"player87": {
"id": "999",
"name": "Bill Clinton",
"trck": "clinton",
"img": "dev/clinton.jpg",
"img2x": "dev/clinton_2x.jpg"
}
},
"coaches": {…},
"manager": {…},
"staff": {…}
}
To start with, I have an array wich consists of one or multiple numbers (for example [888,999]
). They do represent the IDs of selected players (not necessarily all of them, just one or multiple ones).
Now, how can I get all the corresponding data of the associated player (like name, trck, img, etc.) where the key id
equals the number from an array (looping) in the value?
[888,999]
id == 888
first, and then 999
Update: I'm using jQuery.
Upvotes: 1
Views: 594
Reputation: 386654
You could iterate over the keys and build a new object with the found items.
var object1 = { "players": { "player6": { "id": "777", "name": "Barak Obama", "trck": "obama", "img": "dev/obama.jpg", "img2x": "dev/obama_2x.jpg" }, "player23": { "id": "888", "name": "George Bush", "trck": "bush", "img": "dev/bush.jpg", "img2x": "dev/bush_2x.jpg" }, "player87": { "id": "999", "name": "Bill Clinton", "trck": "clinton", "img": "dev/clinton.jpg", "img2x": "dev/clinton_2x.jpg" } }, "coaches": {}, "manager": {}, "staff": {} },
object2 = {},
search = [777, 888];
Object.keys(object1.players).forEach(function (k) {
if (search.indexOf(+object1.players[k].id) > -1) {
object2[k] = object1.players[k];
}
});
document.write('<pre>' + JSON.stringify(object2, 0, 4) + '</pre>');
If you like to get an array, then you could use this
var object1 = { "players": { "player6": { "id": "777", "name": "Barak Obama", "trck": "obama", "img": "dev/obama.jpg", "img2x": "dev/obama_2x.jpg" }, "player23": { "id": "888", "name": "George Bush", "trck": "bush", "img": "dev/bush.jpg", "img2x": "dev/bush_2x.jpg" }, "player87": { "id": "999", "name": "Bill Clinton", "trck": "clinton", "img": "dev/clinton.jpg", "img2x": "dev/clinton_2x.jpg" } }, "coaches": {}, "manager": {}, "staff": {} },
result= [],
search = [777, 888];
Object.keys(object1.players).forEach(function (k) {
if (search.indexOf(+object1.players[k].id) > -1) {
result.push(object1.players[k]);
}
});
document.write('<pre>' + JSON.stringify(result, 0, 4) + '</pre>');
Upvotes: 1
Reputation: 338228
Simple enough (and exactly following your description):
var ids = [888,999];
$.each(ids, function (i, id) {
$.each(data.players, function (key, player) {
if (player.id == id) {
// add player.name, .id, .trck, .img, etc to page
return false; // break the execution of $.each();
}
});
});
Note that player.id == id
makes use of the automatic type conversion in JS, so it's very much intentional that it is not a strict comparison (===
).
Upvotes: 1
Reputation: 3574
Where data
is your object.
Return a "filtered" players object.
var filterPlayers = function(ids){
var obj = {};
for(var player in data.players){
if(ids.indexOf(data.players[player].id) >= 0){
obj[player] = data.players[player];
}
}
return obj;
};
Or, invoke a callback for each match.
var filterPlayers = function(ids, callback){
for(var player in data.players){
if(ids.indexOf(data.players[player].id) >= 0){
callback(data.players[player]);
}
}
};
filterPlayers([888, 999], function(player){
document.write("<span>" + player.name + "</span>");
});
Upvotes: 2
Reputation: 26861
like this:
var players = {
"players": {
"player6": {
"id": "777",
"name": "Barak Obama",
"trck": "obama",
"img": "dev/obama.jpg",
"img2x": "dev/obama_2x.jpg"
},
"player23": {
"id": "888",
"name": "George Bush",
"trck": "bush",
"img": "dev/bush.jpg",
"img2x": "dev/bush_2x.jpg"
},
"player87": {
"id": "999",
"name": "Bill Clinton",
"trck": "clinton",
"img": "dev/clinton.jpg",
"img2x": "dev/clinton_2x.jpg"
}
},
"coaches": {},
"manager": {},
"staff": {}
}
function get_player_by_property(players, property, value){
for (var player_key in players) {
if (players.hasOwnProperty(player_key)) {
if (players[player_key][property] == value)
return players[player_key];
}
}
}
console.log( get_player_by_property(players.players, 'id', '777') );
Upvotes: 1