简体   繁体   中英

Separating multiple data from a JSON file using JavaScript

I am trying to display the values from a JSON file fetched from an API using this code.

const query = 'chicken';
const uri = 'https://trackapi.nutritionix.com/v2/search/instant?query=' + query;
let appid = new Headers();
appid.append('x-app-id', '19421259')
let appkey = new Headers();

appid.append('x-app-key', '54a4e6668518084478d9025d8a5e32a2')

let req = new Request(uri, {
  method: 'GET',
  headers: appid,
  appkey,
});

fetch(req)
  .then((response) => {
    if (response.ok) {
      return response.json();
    } else {
      throw new Error('BAD HTTP stuff');
    }
  })
  .then((jsonData) => {
    console.log(jsonData);
    var jsonString = JSON.stringify(jsonData);
    document.write(jsonString);

  })
  .catch((err) => {
    console.log('ERROR', err.message);
  });

It successfully returns the JSON and makes it into a string however I don't know how I should separate them as the JSON contains multiple data (eg a search for chicken returns multiple chicken meals with varying brands and cooking types) shown below:

{
  "common": [{
        "food_name": "chicken",
        "serving_unit": "oz",
        "tag_name": "chicken",
        "serving_qty": 3,
        "common_type": null,
        "tag_id": "9",
        "photo": {
          "thumb": "https://d2xdmhkmkbyw75.cloudfront.net/9_thumb.jpg"
        },
        "locale": "en_US"
      }, {
        "food_name": "chickensalad",
        "serving_unit": "cup",
        "tag_name": "chicken salad",
        "serving_qty": 0.5,
        "common_type": null,
        "tag_id": "1420",
        "photo": {
          "thumb": "https://d2xdmhkmkbyw75.cloudfront.net/1420_thumb.jpg"
        },
        "locale": "en_US"
      }, {
        "food_name": "chicken salad",
        "serving_unit": "cup",
        "tag_name": "chicken salad",
        "serving_qty": 0.5,
        "common_type": null,
        "tag_id": "1420",
        "photo": {
          "thumb": "https://d2xdmhkmkbyw75.cloudfront.net/1420_thumb.jpg"
        },
        "locale": "en_US"
      }, {
        "food_name": "chicken broth",
        "serving_unit": "cup",
        "tag_name": "broth chicken",
        "serving_qty": 1,
        "common_type": null,
        "tag_id": "3336",
        "photo": {
          "thumb": "https://d2xdmhkmkbyw75.cloudfront.net/3336_thumb.jpg"
        },
        "locale": "en_US"
      }, {
        "food_name": "whole chicken",
        "serving_unit": "chicken",
        "tag_name": "whole chicken",
        "serving_qty": 1,
        "common_type": null,
        "tag_id": "4025",
        "photo": {
          "thumb": "https://d2xdmhkmkbyw75.cloudfront.net/4025_thumb.jpg"
        },
        "locale": "en_US"
      }

How should I implement it so that I could display each food and their respective variables separately?

If I understood it correctly, you are trying to filter the data obtained as a JSON object so it only includes the foods that are held in "food_name" and "tag_name" properties.

In this case, you could write a simple function to do the filtering, like so:

const getFilteredFoodsArray = food => {
  food = food.toLowerCase();
  return common.filter(
    data =>
      data.food_name.toLowerCase().includes(food) ||
      data.tag_name.toLowerCase().includes(food)
  );
};

Then, if you run it like this:

console.log(getFilteredFoodsArray("chicken"))

Your result would be:

[ 
  { food_name: 'chicken',
    serving_unit: 'oz',
    tag_name: 'chicken',
    serving_qty: 3,
    common_type: null,
    tag_id: '9',
    photo:
     { thumb: 'https://d2xdmhkmkbyw75.cloudfront.net/9_thumb.jpg' },
    locale: 'en_US' },
  { food_name: 'chickensalad',
    serving_unit: 'cup',
    tag_name: 'chicken salad',
    serving_qty: 0.5,
    common_type: null,
    tag_id: '1420',
    photo:
     { thumb: 'https://d2xdmhkmkbyw75.cloudfront.net/1420_thumb.jpg' },
    locale: 'en_US' },
  { food_name: 'chicken salad',
    serving_unit: 'cup',
    tag_name: 'chicken salad',
    serving_qty: 0.5,
    common_type: null,
    tag_id: '1420',
    photo:
     { thumb: 'https://d2xdmhkmkbyw75.cloudfront.net/1420_thumb.jpg' },
    locale: 'en_US' },
  { food_name: 'chicken broth',
    serving_unit: 'cup',
    tag_name: 'broth chicken',
    serving_qty: 1,
    common_type: null,
    tag_id: '3336',
    photo:
     { thumb: 'https://d2xdmhkmkbyw75.cloudfront.net/3336_thumb.jpg' },
    locale: 'en_US' },
  { food_name: 'whole chicken',
    serving_unit: 'chicken',
    tag_name: 'whole chicken',
    serving_qty: 1,
    common_type: null,
    tag_id: '4025',
    photo:
     { thumb: 'https://d2xdmhkmkbyw75.cloudfront.net/4025_thumb.jpg' },
    locale: 'en_US' } 
]

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