在Node JS中获取JSON值

时间:2018-08-23 13:27:46

标签: javascript json

我从TMDB收到JSON响应:

{
  "id": 283350,
  "results": [
    {
      "iso_3166_1": "BR",
      "release_dates": [
        {
          "certification": "12",
          "iso_639_1": "pt",
          "note": "Streaming",
          "release_date": "2017-10-25T00:00:00.000Z",
          "type": 4
        }
      ]
    },
    {
      "iso_3166_1": "GB",
      "release_dates": [
        {
          "certification": "12",
          "iso_639_1": "en",
          "release_date": "2015-12-24T00:00:00.000Z",
          "type": 4
        }
      ]
    },
    {
      "iso_3166_1": "SG",
      "release_dates": [
        {
          "certification": "",
          "iso_639_1": "",
          "note": "",
          "release_date": "2015-12-17T00:00:00.000Z",
          "type": 3
        }
      ]
    },
    {
      "iso_3166_1": "TR",
      "release_dates": [
        {
          "certification": "",
          "iso_639_1": "",
          "note": "",
          "release_date": "2015-09-11T00:00:00.000Z",
          "type": 3
        }
      ]
    },
    {
      "iso_3166_1": "AU",
      "release_dates": [
        {
          "certification": "M",
          "iso_639_1": "",
          "release_date": "2015-12-01T00:00:00.000Z",
          "type": 5
        }
      ]
    },
    {
      "iso_3166_1": "PH",
      "release_dates": [
        {
          "certification": "",
          "iso_639_1": "",
          "note": "",
          "release_date": "2015-09-02T00:00:00.000Z",
          "type": 3
        }
      ]
    },
    {
      "iso_3166_1": "US",
      "release_dates": [
        {
          "certification": "PG-13",
          "iso_639_1": "",
          "note": "",
          "release_date": "2015-05-21T00:00:00.000Z",
          "type": 3
        }
      ]
    },
    {
      "iso_3166_1": "KR",
      "release_dates": [
        {
          "certification": "15세이상관람가",
          "iso_639_1": "en",
          "release_date": "2015-11-26T00:00:00.000Z",
          "type": 3
        }
      ]
    },
    {
      "iso_3166_1": "GR",
      "release_dates": [
        {
          "certification": "13",
          "iso_639_1": "",
          "note": "",
          "release_date": "2015-09-02T00:00:00.000Z",
          "type": 3
        }
      ]
    },
    {
      "iso_3166_1": "CA",
      "release_dates": [
        {
          "certification": "",
          "iso_639_1": "",
          "note": "",
          "release_date": "2014-09-11T00:00:00.000Z",
          "type": 3
        }
      ]
    }
  ]
}

现在我真正需要的是仅从美国获得认证,因此我将获得PG-13。 但是由于某种原因,我所做的一切似乎都只是返回undefined并且不会与美国匹配,所以我使它起作用的唯一方法是仅显示[6],这很好,但美国并不总是#6

我该如何实现?

1 个答案:

答案 0 :(得分:1)

您可以使用Array.find()查找具有US认证的对象。 Array.find()方法将找到与给定条件匹配的值。在您的情况下,iso_3166_1的值应为US

var data = {
  "id": 283350,
  "results": [{
    "iso_3166_1": "BR",
    "release_dates": [{
      "certification": "12",
      "iso_639_1": "pt",
      "note": "Streaming",
      "release_date": "2017-10-25T00:00:00.000Z",
      "type": 4
    }]
  }, {
    "iso_3166_1": "GB",
    "release_dates": [{
      "certification": "12",
      "iso_639_1": "en",
      "release_date": "2015-12-24T00:00:00.000Z",
      "type": 4
    }]
  }, {
    "iso_3166_1": "SG",
    "release_dates": [{
      "certification": "",
      "iso_639_1": "",
      "note": "",
      "release_date": "2015-12-17T00:00:00.000Z",
      "type": 3
    }]
  }, {
    "iso_3166_1": "TR",
    "release_dates": [{
      "certification": "",
      "iso_639_1": "",
      "note": "",
      "release_date": "2015-09-11T00:00:00.000Z",
      "type": 3
    }]
  }, {
    "iso_3166_1": "AU",
    "release_dates": [{
      "certification": "M",
      "iso_639_1": "",
      "release_date": "2015-12-01T00:00:00.000Z",
      "type": 5
    }]
  }, {
    "iso_3166_1": "PH",
    "release_dates": [{
      "certification": "",
      "iso_639_1": "",
      "note": "",
      "release_date": "2015-09-02T00:00:00.000Z",
      "type": 3
    }]
  }, {
    "iso_3166_1": "US",
    "release_dates": [{
      "certification": "PG-13",
      "iso_639_1": "",
      "note": "",
      "release_date": "2015-05-21T00:00:00.000Z",
      "type": 3
    }]
  }, {
    "iso_3166_1": "KR",
    "release_dates": [{
      "certification": "15세이상관람가",
      "iso_639_1": "en",
      "release_date": "2015-11-26T00:00:00.000Z",
      "type": 3
    }]
  }, {
    "iso_3166_1": "GR",
    "release_dates": [{
      "certification": "13",
      "iso_639_1": "",
      "note": "",
      "release_date": "2015-09-02T00:00:00.000Z",
      "type": 3
    }]
  }, {
    "iso_3166_1": "CA",
    "release_dates": [{
      "certification": "",
      "iso_639_1": "",
      "note": "",
      "release_date": "2014-09-11T00:00:00.000Z",
      "type": 3
    }]
  }]
};

var USCertification = data.results.find(({iso_3166_1}) => iso_3166_1 == 'US');
console.log(USCertification);
console.log(USCertification.release_dates[0].certification);

使用普通功能

var data = {
  "id": 283350,
  "results": [{
    "iso_3166_1": "BR",
    "release_dates": [{
      "certification": "12",
      "iso_639_1": "pt",
      "note": "Streaming",
      "release_date": "2017-10-25T00:00:00.000Z",
      "type": 4
    }]
  }, {
    "iso_3166_1": "GB",
    "release_dates": [{
      "certification": "12",
      "iso_639_1": "en",
      "release_date": "2015-12-24T00:00:00.000Z",
      "type": 4
    }]
  }, {
    "iso_3166_1": "SG",
    "release_dates": [{
      "certification": "",
      "iso_639_1": "",
      "note": "",
      "release_date": "2015-12-17T00:00:00.000Z",
      "type": 3
    }]
  }, {
    "iso_3166_1": "TR",
    "release_dates": [{
      "certification": "",
      "iso_639_1": "",
      "note": "",
      "release_date": "2015-09-11T00:00:00.000Z",
      "type": 3
    }]
  }, {
    "iso_3166_1": "AU",
    "release_dates": [{
      "certification": "M",
      "iso_639_1": "",
      "release_date": "2015-12-01T00:00:00.000Z",
      "type": 5
    }]
  }, {
    "iso_3166_1": "PH",
    "release_dates": [{
      "certification": "",
      "iso_639_1": "",
      "note": "",
      "release_date": "2015-09-02T00:00:00.000Z",
      "type": 3
    }]
  }, {
    "iso_3166_1": "US",
    "release_dates": [{
      "certification": "PG-13",
      "iso_639_1": "",
      "note": "",
      "release_date": "2015-05-21T00:00:00.000Z",
      "type": 3
    }]
  }, {
    "iso_3166_1": "KR",
    "release_dates": [{
      "certification": "15세이상관람가",
      "iso_639_1": "en",
      "release_date": "2015-11-26T00:00:00.000Z",
      "type": 3
    }]
  }, {
    "iso_3166_1": "GR",
    "release_dates": [{
      "certification": "13",
      "iso_639_1": "",
      "note": "",
      "release_date": "2015-09-02T00:00:00.000Z",
      "type": 3
    }]
  }, {
    "iso_3166_1": "CA",
    "release_dates": [{
      "certification": "",
      "iso_639_1": "",
      "note": "",
      "release_date": "2014-09-11T00:00:00.000Z",
      "type": 3
    }]
  }]
};

var USCertification = data.results.find(function(obj){
  return obj.iso_3166_1 === 'US';
});
console.log(USCertification);
console.log(USCertification.release_dates[0].certification);