如何在Javascript中过滤出数组中类似的属性名称?
我正在做一个从API中提取数据的项目,到目前为止一切进展顺利,但我似乎不知道如何具体过滤成分和测量值 该数组如下所示:如何在Javascript中过滤出数组中类似的属性名称?,javascript,arrays,list,sorting,filter,Javascript,Arrays,List,Sorting,Filter,我正在做一个从API中提取数据的项目,到目前为止一切进展顺利,但我似乎不知道如何具体过滤成分和测量值 该数组如下所示: { “IDMEIN”:“52977”, “标准”:“Corba”, “strDrinkAlternate”:空, “strCategory”:“Side”, “strArea”:“土耳其”, “指令”:“, “strMealThumb”:https://www.themealdb.com/images/media/meals/58oia61564916529.jpg", “s
{
“IDMEIN”:“52977”,
“标准”:“Corba”,
“strDrinkAlternate”:空,
“strCategory”:“Side”,
“strArea”:“土耳其”,
“指令”:“,
“strMealThumb”:https://www.themealdb.com/images/media/meals/58oia61564916529.jpg",
“strTags”:“汤”,
“strYoutube”:https://www.youtube.com/watch?v=VVnZd8A84z4",
“strIngredient1”:“扁豆”,
“strIngredient2”:“洋葱”,
“strIngredient3”:“胡萝卜”,
“strIngredient4”:“番茄泥”,
“strIngredient5”:“孜然”,
“strIngredient6”:“红辣椒”,
“strIngredient7”:“薄荷糖”,
“strIngredient8”:“百里香”,
“strIngredient9”:“黑胡椒”,
“strIngredient10”:“红辣椒片”,
“StringCredit11”:“蔬菜库存”,
“StringCredit12”:“水”,
“StringCredit13”:“海盐”,
“strIngredient14”:“,
“strIngredient15”:“,
“strIngredient16”:“,
“strIngredient17”:“,
“strIngredient18”:“,
“strIngredient19”:“,
“strIngredient20”:“,
“strMeasure1”:“1杯”,
“strMeasure2”:“1大”,
“strMeasure3”:“1大”,
“标准测量4”:“1 tbs”,
“标准测量5”:“2茶匙”,
“标准计量6”:“1茶匙”,
“标准测量7”:“1/2茶匙”,
“标准计量8”:“1/2茶匙”,
“标准措施9”:“1/4茶匙”,
“标准计量10”:“1/4茶匙”,
“strMeasure11”:“4杯”,
“strMeasure12”:“1杯”,
“strMeasure13”:“挤压”,
“strMeasure14”:“,
“strMeasure15”:“,
“strMeasure16”:“,
“strMeasure17”:“,
“strMeasure18”:“,
“strMeasure19”:“,
“strMeasure20”:“,
“strSource”:https://findingtimeforcooking.com/main-dishes/red-lentil-soup-corba/",
“strImageSource”:空,
“strCreateCommonsConfirmmed”:空,
“dateModified”:空
}
请参见下面的代码。您将在成分
数组中选择所有字符串元素
,并在度量值
数组中选择所有标准度量值
const query=async函数(){
试一试{
const response=等待获取(
"https://www.themealdb.com/api/json/v1/1/search.php?s="
);
const data=wait response.json();
控制台日志(数据);
data.Founds.forEach((食谱)=>{
常量recipeName=recipe.strMeal;
const recipeImage=recipe.strmaltumb;
const recipeRegion=recipe.strArea;
const recipecegory=recipe.strCategory;
const recipeInstructions=recipe.strInstructions;
const recipeLink=recipe.strSource;
const components=Object.keys(recipe.filter(k=>k.includes('strIngredient')).map(k=>recipe[k]);
const measures=Object.keys(recipe).filter(k=>k.includes('strMeasure')).map(k=>recipe[k]);
});
}捕获(错误){
console.log(错误);
}
};代码>请参见下面的代码。您将在成分
数组中选择所有字符串元素
,并在度量值
数组中选择所有标准度量值
const query=async函数(){
试一试{
const response=等待获取(
"https://www.themealdb.com/api/json/v1/1/search.php?s="
);
const data=wait response.json();
控制台日志(数据);
data.Founds.forEach((食谱)=>{
常量recipeName=recipe.strMeal;
const recipeImage=recipe.strmaltumb;
const recipeRegion=recipe.strArea;
const recipecegory=recipe.strCategory;
const recipeInstructions=recipe.strInstructions;
const recipeLink=recipe.strSource;
const components=Object.keys(recipe.filter(k=>k.includes('strIngredient')).map(k=>recipe[k]);
const measures=Object.keys(recipe).filter(k=>k.includes('strMeasure')).map(k=>recipe[k]);
});
}捕获(错误){
console.log(错误);
}
};代码>这将使用他们饲料中的所有数据构建配方,但将配料和测量值移动到他们自己的数组中
let recipes=[]
常量查询=异步函数(){
试一试{
const response=等待获取(
"https://www.themealdb.com/api/json/v1/1/search.php?s="
);
const data=wait response.json();
//控制台日志(数据);
//“StringCredit”并将它们全部放入一个单独的数组中,与“strMeasure”相同。
data.Founds.forEach((食谱)=>{
让newRecipe={
成分:[],
测量:[]
}
//console.log(配方)
for(对象项(配方)的常量[键,值]){
设pair={};
对[键]=值;
if(key.indexOf('StringCredit')!=-1)newRecipe.Components.push(成对);
else if(key.indexOf('strMeasure')!=-1)newRecipe.measures.push(成对);
else newRecipe[键]=值;
}
配方。推送(新配方)
});
console.log(食谱)
}捕获(错误){
console.log(错误);
}
};
query()
这将使用他们饲料中的所有数据构建配方,但将配料和测量值移动到他们自己的数组中
let recipes=[]
常量查询=异步函数(){
试一试{
const response=等待获取(
"https://www.themealdb.com/api/json/v1/1/search.php?s="
);
const data=wait response.json();
//控制台日志(数据);
//“StringCredit”并将它们全部放入一个单独的数组中,与“strMeasure”相同。
data.Founds.forEach((食谱)=>{
让newRecipe={
成分:[],
测量:[]
}
//console.log(配方)
for(对象项(配方)的常量[键,值]){
设pair={};
对[键]=值;
if(key.indexOf('StringCredit')!=-1)newRecipe.Components.push(成对);
否则如果(key.indexOf('strMeasure')=
const strIngredient = Object.keys(data.meals)
.filter(x => x.includes('strIngredient'))
.reduce((acc, key) => {
acc[key] = data.meals[key]
}, [])
const strMeasure = Object.keys(data.meals)
.filter(x => x.includes('strMeasure'))
.reduce((acc, key) => {
acc[key] = data.meals[key]
}, [])