如何从数据中获取相应的对象,其中键的值(例如888
)(例如id
)等于循环变量的值(即{{1 }})?
我的数据如下:
id[i] == 888
首先,我有一个由一个或多个数字组成的数组(例如{
"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": {…}
}
)。它们确实代表所选玩家的ID(不一定是所有玩家的ID,只有一个或多个)。
现在,如何获取关联播放器的所有相应数据(如name,trck,img等),其中键[888,999]
等于值中数组(循环)中的数字?< / p>
id
[888,999]
,然后id == 888
更新:我正在使用jQuery。
答案 0 :(得分:2)
import socket
s = socket.socket()
host = "localhost"
port = 2000
s.connect((host , port))
是你的对象。
返回“已过滤”的玩家对象。
data
或者,为每个匹配调用一个回调。
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;
};
答案 1 :(得分:1)
您可以迭代键并使用找到的项目构建新对象。
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>');
&#13;
如果你想获得一个数组,那么你可以使用这个
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>');
&#13;
答案 2 :(得分:1)
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') );
答案 3 :(得分:1)
足够简单(并完全按照您的描述):
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();
}
});
});
请注意player.id == id
在JS中使用自动类型转换,因此非常有意识的是它不是严格的比较(===
)。