遍历此对象的最佳方法是什么?

时间:2020-03-06 09:47:49

标签: javascript loops object

我被困在一个名为Players的对象中,该对象包含播放器数据。我想检查哪个玩家拥有最高的x值,并将其保存在leader变量中,而其他玩家拥有更高的x值时该变量将发生变化。

对象看起来像这样:

var players = {
  '86wjIB7Xbz1tmwlTAAAB': {
     rotation: 0.09999999999999964,    
     x: 579,
     y: 579,
     playerId: '86wjIB7Xbz1tmwlTAAAB'  
   },
  'dWwtnOI8PryXJNDWAAAC': {
    rotation: 0.09999999999999964,    
    x: 488,
    y: 579,
    playerId: 'dWwtnOI8PryXJNDWAAAC'  
  },
 'GZPYpWdrzj9x0-SsAAAD': {
    rotation: -0.09999999999999964,   
    x: 694,
    y: 579,
    playerId: 'GZPYpWdrzj9x0-SsAAAD'  
  }
}

这就是我希望输出显示为

的样子
leader = GZPYpWdrzj9x0;

3 个答案:

答案 0 :(得分:1)

请使用Object.keys

var players = {
    '86wjIB7Xbz1tmwlTAAAB': {
      rotation: 0.09999999999999964,    
      x: 579,
      y: 579,
      playerId: '86wjIB7Xbz1tmwlTAAAB'  
    },
    dWwtnOI8PryXJNDWAAAC: {
      rotation: 0.09999999999999964,    
      x: 488,
      y: 579,
      playerId: 'dWwtnOI8PryXJNDWAAAC'  
    },
    'GZPYpWdrzj9x0-SsAAAD': {
      rotation: -0.09999999999999964,   
      x: 694,
      y: 579,
      playerId: 'GZPYpWdrzj9x0-SsAAAD'  
    }
  }

  const leader = Object.keys(players).reduce((acc, cur) => {
    const obj = players[cur];
    return acc.x < obj.x ? { x:obj.x, leader: obj.playerId } : acc;
  }, { x: 0, leader: "" });

  console.log(leader);

答案 1 :(得分:1)

可以使用以下代码完成

let data= {
      '86wjIB7Xbz1tmwlTAAAB': {
        rotation: 0.09999999999999964,    
         x: 579,
         y: 579,
         playerId: '86wjIB7Xbz1tmwlTAAAB'  
      },
      'dWwtnOI8PryXJNDWAAAC': {
         rotation: 0.09999999999999964,    
         x: 488,
         y: 579,
         playerId: 'dWwtnOI8PryXJNDWAAAC'  
       },
      'GZPYpWdrzj9x0-SsAAAD': {
         rotation: -0.09999999999999964,   
         x: 694,
         y: 579,
         playerId: 'GZPYpWdrzj9x0-SsAAAD'  
     }
    }

    let max = 0;
    let keyParent;
    let keys = Object.keys(data)

    for (var i = 0; i < keys.length; i++){
        if (data[keys[i]].x > max) {
           max = data[keys[i]].x
           keyParent = keys[i]
        }
    }

    console.log(keyParent)

答案 2 :(得分:1)

有几种方法可以做到这一点。周杰伦已经有了一个很好的答案,但是这是另一个答案,还有更多建议:

我们从您的数据开始:

const playersObj = {
  '86wjIB7Xbz1tmwlTAAAB': {
     rotation: 0.09999999999999964,    
     x: 579,
     y: 579,
     playerId: '86wjIB7Xbz1tmwlTAAAB'  ,
   },
  'dWwtnOI8PryXJNDWAAAC': {
    rotation: 0.09999999999999964,    
    x: 488,
    y: 579,
    playerId: 'dWwtnOI8PryXJNDWAAAC',
  },
 'GZPYpWdrzj9x0-SsAAAD': {
    rotation: -0.09999999999999964,   
    x: 694,
    y: 579,
    playerId: 'GZPYpWdrzj9x0-SsAAAD',  
  },
};

现在,我们的首要目标是将这个对象变成对象的集合-我们可以对其进行操作的数组。

最简单的方法是获取对象的“键”(属性名称):

const playerIds = Object.keys(playersObj); // gives us: ['86wjIB7Xbz1tmwlTAAAB', 'dWwtnOI8PryXJNDWAAAC', 'GZPYpWdrzj9x0-SsAAAD']

现在,您可以循环这些键,playerIds并返回实际对象。一种简单的方法:

const players= playerIds.map(playerId => playersObj[playerId]);

这将为我们提供相同的数据,但它们在数组中,并且我们可以在数组上进行操作。

例如让我们按x排序:

players.sort((a, b) => b.x - a.x) // sorts the collection

我们可以做得更进一步,并获得第一个条目:

players.sort((a, b) => b.x - a.x)[0];

最后,我们只需要它的playerId属性:

const leader = players.sort((a, b) => b.x - a.x)[0].playerId; // gives this: 'GZPYpWdrzj9x0-SsAAAD'

整个内容都是可运行的代码段:

const playersObj = {
  '86wjIB7Xbz1tmwlTAAAB': {
    rotation: 0.09999999999999964,
    x: 579,
    y: 579,
    playerId: '86wjIB7Xbz1tmwlTAAAB',
  },
  'dWwtnOI8PryXJNDWAAAC': {
    rotation: 0.09999999999999964,
    x: 488,
    y: 579,
    playerId: 'dWwtnOI8PryXJNDWAAAC',
  },
  'GZPYpWdrzj9x0-SsAAAD': {
    rotation: -0.09999999999999964,
    x: 694,
    y: 579,
    playerId: 'GZPYpWdrzj9x0-SsAAAD',
  },
};
// get the player keys
const playerIds = Object.keys(playersObj);
// turn players into a collection
const players = playerIds.map(playerId => playersObj[playerId]);
const leaderId = players.sort((a, b) => b.x - a.x)[0].playerId;

console.log(`Leader: ${leaderId}`);


或者,按照Felix的建议,您可以跳过前几个步骤。您已经在每个对象上都有了playerId,而不仅仅是在按键上(如我最初的想法),所以您跳过了“ keys()。map()”链,如下所示:

const playersObj = {
  '86wjIB7Xbz1tmwlTAAAB': {
    rotation: 0.09999999999999964,
    x: 579,
    y: 579,
    playerId: '86wjIB7Xbz1tmwlTAAAB',
  },
  'dWwtnOI8PryXJNDWAAAC': {
    rotation: 0.09999999999999964,
    x: 488,
    y: 579,
    playerId: 'dWwtnOI8PryXJNDWAAAC',
  },
  'GZPYpWdrzj9x0-SsAAAD': {
    rotation: -0.09999999999999964,
    x: 694,
    y: 579,
    playerId: 'GZPYpWdrzj9x0-SsAAAD',
  },
};

// turn players into a collection
const players = Object.values(playersObj);

const leaderId = players.sort((a, b) => b.x - a.x)[0].playerId;

console.log(`Leader: ${leaderId}`);

相关问题