在JavaScript中将JSON转换为数组/映射

时间:2020-04-20 17:15:20

标签: javascript arrays node.js json loops

我有一个JSON响应,其中包含用户播放音频文件的时间。对于每个音频会话,我都有:

   {"session": {"checkpoints": [
   {"status": "started", "position": 0, "createdAt": "2020-02- 
   20T16:55:37.795Z"}, 
   {"status": "paused", "position": 195, "createdAt": "2020-02- 
   20T16:58:53.917Z"}, 
   {"status": "stopped", "position": 195, "createdAt": "2020-02- 
   20T17:01:04.230Z"},
   {"status": "closed", "position": 195, "createdAt": "2020-02- 
   20T17:01:04.250Z"}], "totalTime": 195}}

我想映射所有totalTime值并找到最大值。做这个的最好方式是什么? 我最初所做的是:

      let maxValue = 0
      let data = JSON.parse(audios).data
      for (let totalTime in data) {
        if (data.hasOwnProperty(totalTime)) {
          let value = data[totalTime]
          if (value > maxValue) {
            maxValue = value
          }
        }
      }

2 个答案:

答案 0 :(得分:-1)

您可以像这样以最大totalTime进行会话

// created 2 sessions, with different totalTime as example
const data = [{
    "session": {
      "checkpoints": [{
          "status": "started",
          "position": 0,
          "createdAt": "2020-02-20T16:55:37.795Z"
        },
        {
          "status": "paused",
          "position": 195,
          "createdAt": "2020-02-20T16:58:53.917Z"
        },
        {
          "status": "stopped",
          "position": 195,
          "createdAt": "2020-02-20T17:01:04.230Z"
        },
        {
          "status": "closed",
          "position": 195,
          "createdAt": "2020-02-20T17:01:04.250Z"
        }
      ],
      "totalTime": 195
    }
  },
  {
    "session": {
      "checkpoints": [{
          "status": "started",
          "position": 0,
          "createdAt": "2020-02-20T16:55:37.795Z"
        },
        {
          "status": "paused",
          "position": 195,
          "createdAt": "2020-02-20T16:58:53.917Z"
        },
        {
          "status": "stopped",
          "position": 195,
          "createdAt": "2020-02-20T17:01:04.230Z"
        },
        {
          "status": "closed",
          "position": 195,
          "createdAt": "2020-02-20T17:01:04.250Z"
        }
      ],
      "totalTime": 193
    }
  }
]

const maxSession = data.sort((a, b) => a.totalTime - b.totalTime)[0]
console.log('maxSession', maxSession)

答案 1 :(得分:-1)

好吧,我假设您有多个条目,例如您作为示例给出的条目,它们都位于一个数组中。就像这样:

var data = [
{"session": {"checkpoints": [
   {"status": "started", "position": 0, "createdAt": "2020-02- 
   20T16:55:37.795Z"}, 
   {"status": "paused", "position": 195, "createdAt": "2020-02- 
   20T16:58:53.917Z"}, 
   {"status": "stopped", "position": 195, "createdAt": "2020-02- 
   20T17:01:04.230Z"},
   {"status": "closed", "position": 195, "createdAt": "2020-02- 
   20T17:01:04.250Z"}], "totalTime": 195}},
{"session": {"checkpoints": [
   {"status": "started", "position": 0, "createdAt": "2020-02- 
   20T16:55:37.795Z"}, 
   {"status": "paused", "position": 195, "createdAt": "2020-02- 
   20T16:58:53.917Z"}, 
   {"status": "stopped", "position": 195, "createdAt": "2020-02- 
   20T17:01:04.230Z"},
   {"status": "closed", "position": 195, "createdAt": "2020-02- 
   20T17:01:04.250Z"}], "totalTime": 195}},
{"session": {"checkpoints": [
   {"status": "started", "position": 0, "createdAt": "2020-02- 
   20T16:55:37.795Z"}, 
   {"status": "paused", "position": 195, "createdAt": "2020-02- 
   20T16:58:53.917Z"}, 
   {"status": "stopped", "position": 195, "createdAt": "2020-02- 
   20T17:01:04.230Z"},
   {"status": "closed", "position": 195, "createdAt": "2020-02- 
   20T17:01:04.250Z"}], "totalTime": 195}}
]

如果我是对的,这就是您想要做的:

  1. 排序数组:

var sorted = data.sort((a, b) => b.session.totalTime - a.session.totalTime);

对于每对对象,您将访问要基于其对数组进行排序的值。注意,在减法中我已经颠倒了ba。那是因为您想对它进行向下排序,所以更大的价值将首先出现。

  1. 采用第一个条目,它将是具有最大totalTime的条目:

var entry = sorted[0];

  1. 从条目中提取所需的值:

var maxTotalTime = entry.session.totalTime; console.log(maxTotalTime);


我已经显示了上述步骤,因为您请求了最佳解决方案,但是这里是如何纠正您尝试使用的代码的方法:

  let maxValue = 0
  let data = JSON.parse(audios).data
  for (let entry of data) {
      let value = entry.session.totalTime;
      if (value > maxValue) {
        maxValue = value;
      }
  }