简体   繁体   中英

How can I pull a key value from JSON API only if another key name within the group matches?

So this is whats on the JSON file:

{
  "page": 1,
  "total_pages": 10,
  "listings": [
    {
      "name": "Bob",
      "occu": "Entry",
      "team": "Blue",
      "sec": 3,
      "days": 16
    },
    {
      "name": "Tom",
      "occu": "Advance",
      "team": "Main",
      "sec": 1,
      "days": 23
    },

This continues on with hundreds of other entries...

How can I pull the value of "days" only if the "name" is Tom for example.

Sorry if this is a primitive question, im just getting started on developing and im working on a quick project that will help my local sports team and im quite not that advanced with scripting or APIs. Thanks

To pull the value of "days" only if the "name" is Tom need to filter by 'Tom' name and then map days

 let listings = [ { "name": "Bob", "occu": "Entry", "team": "Blue", "sec": 3, "days": 16 }, { "name": "Tom", "occu": "Advance", "team": "Main", "sec": 1, "days": 23 }, { "name": "Tom", "occu": "Advanddce", "team": "Maiddn", "sec": 1, "days": 55 } ]; const result = listings.filter(listing=> listing.name=="Tom").map(listing => listing.days); console.log(result); 

You can make a function like this which loops through an array ( listings ) of objects:

getDaysFromListings = (listings, name) => {
    for (let listing of jsonObj.listings) {
        if (listing.name === name) {
            return listing.days;
        }
    }

    // didn't find 'Tom', return empty string
    return '';
}

Then, call your function like this:

// assuming your entire json object is stored in a variable called "json"
let days = getDaysFromListings(json.listings, 'Tom');

Since your variable days comes as a string , you can turn this into a number with +days .

Edit : To add the variable days to html, first, let's assume you want to add the text to a div that looks like this:

<div id="target"></div>

You can use javascript to add the days variable here like this:

// assume you already stored the data in variable
document.getElementById('target').innerHTML = days;

Note : You have to load this javascript after you load the html div above.

As there are many ways to achieve this and am certain this may not be the best method, one way is using Array.prototype.find https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/find

var array1 = [{
      "name": "Bob",
      "occu": "Entry",
      "team": "Blue",
      "sec": 3,
      "days": 16
    }, {
      "name": "Tom",
      "occu": "Advance",
      "team": "Main",
      "sec": 1,
      "days": 23
    }];

var found = array1.find(function(element) {
  return element.name === "Bob";
});

console.log(found.days);

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