简体   繁体   中英

How do I filter out values from these JSON objects

I'm using a online API that returns to me JSON code and I don't know how to filter out the "name" value in the JSON code. The JSON that I'm trying to filter is in many objects and I've tried many different ways. How do I get the values of "name" out of each object? I've tried to do .people.name but I always get a blank output or it would just say "[object Object][object Object][object Object][object Object][object Object]"

JSON response:

{
  "message": "success",
  "number": 6,
  "people": [
    {
      "craft": "ISS",
      "name": "Oleg Kononenko"
    },
    {
      "craft": "ISS",
      "name": "David Saint-Jacques"
    },
    {
      "craft": "ISS",
      "name": "Anne McClain"
    },
    {
      "craft": "ISS",
      "name": "Alexey Ovchinin"
    },
    {
      "craft": "ISS",
      "name": "Nick Hague"
    },
    {
      "craft": "ISS",
      "name": "Christina Koch"
    }
  ]
}

My code in NodeJS:

request('http://api.open-notify.org/astros.json', (error, response, html) => {

        if (!error && response.statusCode == 200)
        {
            let astroJSON = JSON.parse(html);

            let astroNum = astroJSON.number;
            let astroNames = JSON.stringify(astroJSON.people); // This is what I need help with!
            console.log("Number: " + astroNum);
            console.log("Crew names: " + astroNames); // Return the JSON response that I sent above. 
        }
    });

Try using Array#prototype#map

 const data = `{ "message": "success", "number": 6, "people": [ { "craft": "ISS", "name": "Oleg Kononenko" }, { "craft": "ISS", "name": "David Saint-Jacques" }, { "craft": "ISS", "name": "Anne McClain" }, { "craft": "ISS", "name": "Alexey Ovchinin" }, { "craft": "ISS", "name": "Nick Hague" }, { "craft": "ISS", "name": "Christina Koch" } ] }`; const obj = JSON.parse(data); const astroNames = obj.number; // Join the crew names with a comma const crew = obj.people.map(x => x.name).join(', '); console.log(crew);

Change your code to this:

request('http://api.open-notify.org/astros.json', (error, response, html) => {

        if (!error && response.statusCode == 200)
        {
            let astroJSON = JSON.parse(html);

            let astroNum = astroJSON.number;
            let astroNames = astroJSON.people.map(x => x.name).join(', ');
            console.log("Number: " + astroNum);
            console.log("Crew names: " + astroNames); // Return the JSON response that I sent above. 
        }
    });

You must map over the array. See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map

eg

astroJSON.people.map(person => person.name)

Will log an array of the names of the people:

["Oleg Kononenko", "David Saint-Jacques", "Anne McClain", "Alexey Ovchinin", "Nick Hague", "Christina Koch"]

In order to remove the brackets and the comas you can use

astroNames.join(' ')

which returns the string "Oleg Kononenko David Saint-Jacques Anne McClain Alexey Ovchinin Nick Hague Christina Koch"

See Array.prototype.join

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM