首页 > 解决方案 > 比较 NodeJS 中的 JSON 对象

问题描述

你如何在 NodeJS 中读取和比较 JSON 数据?

{
  "currentcount": 150,
  "hasStorm": false,
  "players": [
    {
      "world": "earth",
      "armor": 0,
      "name": "ceduuu",
      "x": 5400.0,
      "y": 115.0,
      "health": 0,
      "z": -11309.0,
      "sort": 1,
      "type": "player",
      "account": "ceduuu"
    },
    {
      "world": "-some-other-bogus-world-",
      "armor": 0,
      "name": "Egga_",
      "x": 0.0,
      "y": 64.0,
      "health": 0,
      "z": 0.0,
      "sort": 1,
      "type": "player",
      "account": "Egga_"
    },

有 150 个个人数据,我想暂时将 X、Z 和帐户存储在一个变量中,以便我进行比较。但是,我将如何检查第一个玩家的数据以及如何检查第二个玩家的数据?

标签: node.jsjson

解决方案


目前尚不清楚您要将对象与什么进行比较,但这里有一些代码可以帮助您入门。

const comparePlayers = (players) => {
  // iterates over each object
  players.forEach(player => {
    // destructure object
    const { x, z, account } = player;

    // your compare players logic

  });
};

comparePlayers(jsonData.players);

推荐阅读