0

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

jelhan
  • 6,149
  • 1
  • 19
  • 35
  • 1
    what have you tried so far? – Luke Jun 04 '18 at 21:44
  • Possible duplicate of [How do I check if an array includes an object in JavaScript?](https://stackoverflow.com/questions/237104/how-do-i-check-if-an-array-includes-an-object-in-javascript) or [Find object by id in an array of JavaScript objects](https://stackoverflow.com/q/7364150) – Heretic Monkey Jun 04 '18 at 21:51

3 Answers3

1

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);
Mohammad Ali Rony
  • 4,695
  • 3
  • 19
  • 33
0

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.

Blundering Philosopher
  • 6,245
  • 2
  • 43
  • 59
  • How do I get that return value and get it displayed on the page via html? Looks like I have everything set up right, no errors in console log, I just need to link it. I tried a couple things like getElementById but im assuming thats wrong since nothing displays. Thanks for the help. – Oscar Sandoval Jun 05 '18 at 03:31
0

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