Retrieving JSON data with Node.js

Receiving 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
        }
      ]
    }
  ]
}

I am trying to extract the certification specifically from the US, like PG-13. However, my attempts are returning undefined and failing to match with the US. The only workaround I found was to display only [6], which shows the correct value for now, but the position of the US may change. How can I solve this issue?

Answer №1

To locate the object with "US" certification, you can utilize the Array.find() method. This function will search for a value that meets the specified condition, in this case, where the iso_3166_1 property has a 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
    }]
  },
  ... (remaining array elements)
};

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
    }]
  },
  ... (remaining array elements)
};

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

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

The error message "Error: 'x' is not a defined function or its output is not iterable"

While experimenting, I accidentally discovered that the following code snippet causes an error in V8 (Chrome, Node.js, etc): for (let val of Symbol()) { /*...*/ } TypeError: Symbol is not a function or its return value is not iterable I also found out ...

Spring Boot Ajax Parse Error - issue with returning object to Ajax success function

When I make a jQuery ajax call to my Spring controller, I am trying to send back an object to fill out a form in an iziModal. The data gets sent from the browser to the controller and runs through the method, but then I hit a roadblock. I'm having tro ...

Are there any JavaScript libraries available that can mimic SQLite using localStorage?

My current application makes use of SQLite for storage, but I am looking to switch it up to make it compatible with Firefox and other browsers. I've been considering localStorage as an option. However, I've noticed that localStorage lacks some o ...

The www file is only loaded by Node Inspector when the preload setting is turned off

Whenever I start node-inspector The node-inspector browser window successfully loads all the files. However, when I use node-inspector --preload=false Only my bin/www file is loaded on the node-inspector window. Oddly enough, my colleagues are not f ...

Best practice for structuring an object with multiple lengthy string elements in the GCP Datastore Node Library

My JavaScript object is structured like this: const data = { title: "short string", descriptions: [ "Really long string...", "Really long string..." ] } I need to exclude the long strings from the indexes, but I ...

Tips for handling a disabled button feature in Python Selenium automation

When trying to click this button: <button id="btn-login-5" type="button" class="m-1 btn btn-warning" disabled="">Update</button> I need to remove the disable attribute to make the button clickable. This ...

A step-by-step guide on making a web API request to propublica.org using an Angular service

Currently, I am attempting to extract data from propublica.org's congress api using an Angular 8 service. Despite being new to making Http calls to an external web api, I am facing challenges in comprehending the documentation available at this link: ...

The issue of Nodejs Messenger broadcast message functionality being inoperative

Trying to initiate a broadcast through my Facebook Messenger bot, I have implemented the following code: if (subscribe === true) { // Initiate HTTP request to Messenger Platform request({ "uri": "https://graph.facebook.com/v2.11/me/broadcast_messa ...

When working with an Angular application, IE9 may display the error message: "An internal error occurred in the Microsoft Internet extensions"

My application is encountering an issue in IE9: Error: A Microsoft Internet Extensions internal error has occurred. Error: Access is denied. When using IE's dev tools for debugging, the issue seems to be related to localStorage. if (localStorage) ...

What is the best way to transform a string into emojis using TypeScript or JavaScript?

Looking to convert emoji from string in typescript to display emoji in html. Here is a snippet of the Typescript file: export class Example { emoji:any; function(){ this.emoji = ":joy:" } } In an HTML file, I would like it to dis ...

Stop Internet Explorer from automatically scrolling when it gains focus

Please access this fiddle using internet explorer (11): https://jsfiddle.net/kaljak/yw7Lc1aw/1/ When the page loads, the <p> tag is focused and Internet Explorer slightly scrolls the element so that the table border becomes invisible... document.qu ...

JSON Date Format

I'm facing an issue where I am unable to retrieve the current date using new Date() because it is in JSON format. This particular code was written using MVC C#. The date appears as \/Date(1531364413000)\/. The dates stored in the database ...

How can I change the background color of a parent div when hovering over a child element using JavaScript

I am working on a task that involves three colored boxes within a div, each with a different color. The goal is to change the background-color of the parent div to match the color of the box being hovered over. CSS: .t1_colors { float: left; wid ...

Displaying dynamic SVG with AngularJS

Trying to generate an SVG based on data from the scope, but encountering issues with rendering - it comes out empty or displays 'NaN' for some unknown reason. https://i.sstatic.net/vO70i.png Additionally, errors are popping up right after the r ...

I'm looking for a button that, when clicked, will first verify the password. If the password is "abcd," it will display another submit button. If not, it

<form action="#" method="post" target="_blank"> <div id = "another sub"> </br> <input type = "password" size = "25px" id="pswd"> </br> </div> <button><a href="tabl ...

Verifying updates in the watchlist once data has been initialized upon mounting

When trying to edit a component, the data is loaded with mounted in the following way: mounted () { var sectionKey = this.$store.getters.getCurrentEditionKey this.table = _.clone(this.$store.getters.getDocumentAttributes[sectionKey]) this.ta ...

The request to http://localhost:3000/cartdata has encountered an internal server error (500) in the isAxiosError.js file at line

Error Image I'm currently working on developing a shopping cart feature, and I've encountered an issue when transferring data from the client side to the server side. Despite the cart data being successfully updated in the session, an internal se ...

Creating diverse content for various tabs using JavaScript

I have developed a code that uses a for loop to generate tabs based on user input. var tabs = ""; var y = 1; for (var x = 0; x < tabNum; x++) { tabs += "<li class = 'tabbers'>" + "<a href='#tab'>Tab</a>" + "& ...

Generating JSON format using PHP and MySQL

Is it possible to generate Json from PHP that produces results similar to the following structure? series: [{ name: "Brands", colorByPoint: true, data: [{ name: "Microsoft Internet Explorer", y: 56.33, ...

What is the process for cancelling an interval when it is disabled in my configuration file?

To automate a bot, I want it to stop running an interval if the configuration file specifies "off" and continue running if it says "on". I attempted this: Using discord.js: config.Interval = setInterval(() => { WallCheck.send(WallCheckemb ...