1

I am getting a JSON response from TMDB:

{
  "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
        }
      ]
    }
  ]
}

Now what i really need is to get the certification only from the US, so i would get PG-13. But for some reason whatever i do seems to just return undefined and will not match with the US, the only way i got this to work was showing only [6] which is fine, but US will not always be #6

How can I achieve this?

1
  • 1
    I think you are missing your javascript code for this question to be clearer. Commented Aug 23, 2018 at 13:44

1 Answer 1

2

You can use Array.find() to find the object with US certification. The Array.find() method will find a value that matches the given condition. In your case iso_3166_1 should have value of 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);

USING PLAIN FUNCTION

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);

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you very much. This was exactly what i was looking for
@ThomasPetersen glad to help

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.