Javascript 从具有类似键的JSON对象检索数据

Javascript 从具有类似键的JSON对象检索数据,javascript,jquery,json,Javascript,Jquery,Json,我有从AJAX GET请求返回的JSON数据。此请求返回不同的套餐以及如何制作,包括所需的配料和尺寸。JSON数据片段(为了便于查看,删除了一些key:value): JAVASCRIPT/JQUERY代码 $.each($(response), function(key, val) { let mealName = val.strMeal; let mealCategory = val.strCategory; let mealInstructions =

我有从AJAX GET请求返回的JSON数据。此请求返回不同的套餐以及如何制作,包括所需的配料和尺寸。JSON数据片段(为了便于查看,删除了一些key:value):

JAVASCRIPT/JQUERY代码

$.each($(response), function(key, val) {
      let mealName = val.strMeal;
      let mealCategory = val.strCategory;
      let mealInstructions = val.strInstructions;
      let mealIngredient1 = val.strIngredient1;
      let mealIngredient2 = val.strIngredient2;
      ... // Variables continued
      ...
      let mealIngredient20 = val.strIngredient20;

      let mealMeasure1 = val.strMeasure1;
      let mealMeasure2 = val.strMeasure2;
      ... // Variables continued
      ...
      let mealMeasure20 = val.strMeasure20;

      console.log(mealName);
      console.log(mealCategory);
      console.log(mealInstructions);
      console.log(mealIngredient1);
      console.log(mealIngredient2);
      console.log(mealMeasure1);
      console.log(mealMeasure2);
    });
正如你所看到的,每顿饭最多可以有20种成分和尺寸,而且会有所不同。因此,我的问题是,在获取每种成分和度量值时,是否有比检索每种成分和度量值并将其存储在单个变量中更好或更有效的方法,同时还要去除不会使用的空变量或空变量?

请检查并:

const数据={
“IDMEIN”:“52768”,
“strMeal”:“苹果果馅饼”,
“strDrinkAlternate”:空,
“strCategory”:“甜点”,
“strIngredient1”:“消化饼干”,
“strIngredient2”:“黄油”,
“strIngredient3”:“Bramley苹果”,
“strIngredient4”:“黄油,软化”,
“strIngredient5”:“砂糖”,
“strIngredient6”:“自由放养的鸡蛋,打过的”,
“strIngredient7”:“磨碎的杏仁”,
“strIngredient8”:“杏仁提取物”,
“strIngredient9”:“杏仁片”,
“strIngredient10”:“,
“strIngredient11”:“,
“strIngredient12”:“,
“strIngredient13”:“,
}
//从响应中获取[key,value]项的数组
常量属性=Object.entries(数据);
//筛选出值为空的项目
const nonEmpty=properties.filter(([[uu,value])=>value);
//用剩下的做你需要做的
nonEmpty.map(([name,value])=>console.log(`${name}:${value}`)签出并:

const数据={
“IDMEIN”:“52768”,
“strMeal”:“苹果果馅饼”,
“strDrinkAlternate”:空,
“strCategory”:“甜点”,
“strIngredient1”:“消化饼干”,
“strIngredient2”:“黄油”,
“strIngredient3”:“Bramley苹果”,
“strIngredient4”:“黄油,软化”,
“strIngredient5”:“砂糖”,
“strIngredient6”:“自由放养的鸡蛋,打过的”,
“strIngredient7”:“磨碎的杏仁”,
“strIngredient8”:“杏仁提取物”,
“strIngredient9”:“杏仁片”,
“strIngredient10”:“,
“strIngredient11”:“,
“strIngredient12”:“,
“strIngredient13”:“,
}
//从响应中获取[key,value]项的数组
常量属性=Object.entries(数据);
//筛选出值为空的项目
const nonEmpty=properties.filter(([[uu,value])=>value);
//用剩下的做你需要做的

nonEmpty.map(([name,value])=>console.log(`${name}:${value}`)您的StringCredit对象属性应该是一个数组StringCredit[],您可以动态地对其进行迭代,StringCredit[]也是如此。现在编写代码时,您正在设置许多不必要的属性,具体取决于给定的recepie。

您的StringRedent#对象属性应该是一个数组StringRedent[],您可以动态地对其进行迭代,与strMeasure[]类似。现在编写代码时,根据给定的recepie设置了许多不必要的属性。

您可以使用对象分解和rest操作符来获取其余部分

const数据={
“IDMEIN”:“52768”,
“strMeal”:“苹果果馅饼”,
“strDrinkAlternate”:空,
“strCategory”:“甜点”,
“strIngredient1”:“消化饼干”,
“strIngredient2”:“黄油”,
“strIngredient3”:“Bramley苹果”,
“strIngredient4”:“黄油,软化”,
“strIngredient5”:“砂糖”,
“strIngredient6”:“自由放养的鸡蛋,打过的”,
“strIngredient7”:“磨碎的杏仁”,
“strIngredient8”:“杏仁提取物”,
“strIngredient9”:“杏仁片”,
“strIngredient10”:“,
“strIngredient11”:“,
“strIngredient12”:“,
“strIngredient13”:“,
“strIngredient14”:“,
“strIngredient15”:“,
“strIngredient16”:空,
“strIngredient17”:空,
“strIngredient18”:空,
“strIngredient19”:空,
“strIngredient20”:空,
“标准计量1”:“175g\/6oz”,
“标准计量2”:“75克\/3盎司”,
“标准计量3”:“200g\/7oz”,
“标准计量4”:“75克\/3盎司”,
“标准计量5”:“75克\/3盎司”,
“strMeasure6”:“2”,
“标准计量7”:“75克\/3盎司”,
“标准计量8”:“1茶匙”,
“标准测量值9”:“50g\/1\u00beoz”,
“strMeasure10”:“,
“strMeasure11”:“,
“strMeasure12”:“,
“strMeasure13”:“,
“strMeasure14”:“,
“strMeasure15”:“,
“strMeasure16”:空,
“strMeasure17”:空,
“strMeasure18”:空,
“strMeasure19”:空,
“strMeasure20”:空
};
常量{IDMEIN,strMeal,strDrinkAlternate,strCategory,…rest}=数据;

控制台日志(rest)您可以使用对象分解和rest操作符来获取剩余部分

const数据={
“IDMEIN”:“52768”,
“strMeal”:“苹果果馅饼”,
“strDrinkAlternate”:空,
“strCategory”:“甜点”,
“strIngredient1”:“消化饼干”,
“strIngredient2”:“黄油”,
“strIngredient3”:“Bramley苹果”,
“strIngredient4”:“黄油,软化”,
“strIngredient5”:“砂糖”,
“strIngredient6”:“自由放养的鸡蛋,打过的”,
“strIngredient7”:“磨碎的杏仁”,
“strIngredient8”:“杏仁提取物”,
“strIngredient9”:“杏仁片”,
“strIngredient10”:“,
“strIngredient11”:“,
“strIngredient12”:“,
“strIngredient13”:“,
“strIngredient14”:“,
“strIngredient15”:“,
“strIngredient16”:空,
“strIngredient17”:空,
“strIngredient18”:空,
“strIngredient19”:空,
“strIngredient20”:空,
“标准计量1”:“175g\/6oz”,
“标准计量2”:“75克\/3盎司”,
“标准计量3”:“200g\/7oz”,
“标准计量4”:“75克\/3盎司”,
“标准计量5”:“75克\/3盎司”,
“strMeasure6”:“2”,
“标准计量7”:“75克\/3盎司”,
“标准计量8”:“1茶匙”,
“标准测量值9”:“50g\/1\u00beoz”,
“strMeasure10”:“,
“strMeasure11”:“,
“strMeasure12”:“,
“strMeasure13”:“,
“strMeasure14”:“,
“strMeasure15”:“,
“strMeasure16”:空,
“strMeasure17”:空,
“strMeasure18”:空,
“strMeasure19”:空,
“strMeasure20”:空
};
常数、标准、标准、标准、标准范畴
$.each($(response), function(key, val) {
      let mealName = val.strMeal;
      let mealCategory = val.strCategory;
      let mealInstructions = val.strInstructions;
      let mealIngredient1 = val.strIngredient1;
      let mealIngredient2 = val.strIngredient2;
      ... // Variables continued
      ...
      let mealIngredient20 = val.strIngredient20;

      let mealMeasure1 = val.strMeasure1;
      let mealMeasure2 = val.strMeasure2;
      ... // Variables continued
      ...
      let mealMeasure20 = val.strMeasure20;

      console.log(mealName);
      console.log(mealCategory);
      console.log(mealInstructions);
      console.log(mealIngredient1);
      console.log(mealIngredient2);
      console.log(mealMeasure1);
      console.log(mealMeasure2);
    });