获取key的值等于数组值的对象

时间:2016-05-12 14:02:40

标签: javascript jquery

如何从数据中获取相应的对象,其中键的值(例如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
  • 的所有玩家的数据
  • 使用此数据(name,id,trck,img等)向页面添加带有这些值的HTML标记

更新:我正在使用jQuery。

4 个答案:

答案 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)

您可以迭代键并使用找到的项目构建新对象。

&#13;
&#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": {} },
    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;
&#13;
&#13;

如果你想获得一个数组,那么你可以使用这个

&#13;
&#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;
&#13;
&#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中使用自动类型转换,因此非常有意识的是它不是严格的比较(===)。