Javascript 试图得到所有相似的对象

Javascript 试图得到所有相似的对象,javascript,object,Javascript,Object,这是json数据,但不是这样做: {"drinks":[{"idDrink":"13060","strDrink":"Margarita","strVideo":null,"strCategory":"Ordinary Drink","strIBA":"Contemporary Classics","strAlcoholic":"Alcoholic","strGlass":"Cocktail glass","strInstructions":"Rub the rim of the glass w

这是json数据,但不是这样做:

{"drinks":[{"idDrink":"13060","strDrink":"Margarita","strVideo":null,"strCategory":"Ordinary Drink","strIBA":"Contemporary Classics","strAlcoholic":"Alcoholic","strGlass":"Cocktail glass","strInstructions":"Rub the rim of the glass with the lime slice to make the salt stick to it. Take care to moisten only the outer rim and sprinkle the salt on it. The salt should present to the lips of the imbiber and never mix into the cocktail. Shake the other ingredients with ice, then carefully pour into the glass.","strDrinkThumb":"http:\/\/www.thecocktaildb.com\/images\/media\/drink\/wpxpvu1439905379.jpg","strIngredient1":"Tequila","strIngredient2":"Triple sec","strIngredient3":"Lime juice","strIngredient4":"Salt","strIngredient5":"","strIngredient6":"","strIngredient7":"","strIngredient8":"","strIngredient9":"","strIngredient10":"","strIngredient11":"","strIngredient12":"","strIngredient13":"","strIngredient14":"","strIngredient15":"","strMeasure1":"1 1\/2 oz ","strMeasure2":"1\/2 oz ","strMeasure3":"1 oz ","strMeasure4":"","strMeasure5":"","strMeasure6":"","strMeasure7":"","strMeasure8":"","strMeasure9":"","strMeasure10":"","strMeasure11":"","strMeasure12":"","strMeasure13":"","strMeasure14":"","strMeasure15":"","dateModified":"2015-08-18 14:42:59"}]}

我宁愿找一个更整洁的方法来做这件事。我尝试过循环,但作为新手,我似乎找不到一个有效的。编号的对象对我来说真的很新,不确定如何处理它们,例如-strIngredient12或strMeasure7这听起来像是一个好方法吗

des.innerHTML = cocktails.strIngredient1 + " " + cocktails.strMeasure1 + " - " + cocktails.strIngredient2 + " " + cocktails.strMeasure2 + " - " + cocktails.strIngredient3 + " " + cocktails.strMeasure3 + " - " + cocktails.strIngredient4 + " " + cocktails.strMeasure4 + "  " + cocktails.strIngredient5 + "  " + cocktails.strMeasure5 + "  " + cocktails.strInstructions;
const obj={饮料:[{idDrink:13060,strDrink:Margarita,strVideo:null,strCategory:普通饮料,strIBA:当代经典,strAlcoholic:酒精,strGlass:鸡尾酒杯,Strim说明:用莱姆片摩擦杯沿,使盐粘在杯沿上。注意只润湿外缘,并在其上撒盐。盐应呈现在嘴唇上喝下饮料,切勿混入鸡尾酒。加冰摇匀其他成分,然后小心地倒入杯中。strDrinkThumb:http:\/\/www.thecocktaildb.com\/images\/media\/drink\/wpxpvu1439905379.jpg,strIngredient 1:龙舌兰,strIngredient 2:三秒,strIngredient 3:酸橙汁,strIngredient 4:盐,strIngredient 5:,strIngredient 6:,strIngredient7:,StringResident 8:,StringResident 9:,StringResident 10:,StringResident 11:,StringResident 12:,StringResident 13:,StringResident 14:,StringResident 15:,strMeasure1:1\/2盎司,strMeasure2:1\/2盎司,strMeasure3:1盎司,strMeasure4:,strMeasure5:,strMeasure6:,strMeasure7:,strMeasure8:,strMeasure9:,strMeasure10:,strMeasure11:rMeasure15:,日期修改:2015-08-1814:42:59}] Object.getOwnPropertyNamesobj.drinks[0]。forEach 函数val,idx,数组{ document.writelnval+'-'+对象饮料[0][val]; }
如果你能够利用一些ES6,你可以使用对象的值,这将减少重复并将它们放在一个数组中,这将消除连接这么多字符串的需要

const des=document.getElementById'des'; 康斯特鸡尾酒={ 饮料:[{ 国际电话号码:13060, strDrink:玛格丽塔, strVideo:null, 标准分类:普通饮料, 斯特里巴:当代经典, 酒精的, 鸡尾酒杯, 用法:用石灰片摩擦杯口,使盐粘在杯口上。注意只弄湿杯口,然后撒上盐。盐应出现在杯口,切勿混入鸡尾酒。用冰块摇动其他成分,然后小心地倒入杯内。, strinkthumb:http:\/\/www.thecocktaildb.com\/images\/media\/drink\/wpxpvu1439905379.jpg, strIngredient1:龙舌兰, StringCredit2:三秒, 第三条:酸橙汁, 第四条:盐, strIngredient5:, strIngredient6:, strIngredient7:, strIngredient8:, strIngredient9:, strIngredient10:, strIngredient11:, strIngredient12:, strIngredient13:, strIngredient14:, strIngredient15:, 标准测量值1:1\/2盎司, 标准计量2:1\/2盎司, 标准测量值3:1盎司, strMeasure4:, strMeasure5:, strMeasure6:, strMeasure7:, strMeasure8:, strMeasure9:, strMeasure10:, strMeasure11:, strMeasure12:, strMeasure13:, strMeasure14:, strMeasure15:, 修改日期:2015-08-1814:42:59 }] } const{strIngredient1,strIngredient1,strIngredient2,strIngredient2,strIngredient3,strIngredient4,strIngredient4,strIngredient5,strMeasure5,StringInstructions}=鸡尾酒。饮料[0]; des.innerHTML=${strIngredient1}${strIngredient1}${strIngredient2}${strIngredient2}${strIngredient3}${strIngredient3}${strIngredient4}${strIngredient4}${strIngredient5}${strIngredient5}${StringInstructions};
对于循环JSON数组,请查看它。它工作得很好!!!非常感谢!学习新方法总是最好的!!很高兴我能提供帮助!如果您认为答案正确,请不要忘记将其标记为正确。
let drinks = {"drinks":[{"idDrink":"13060","strDrink":"Margarita","strVideo":null,"strCategory":"Ordinary Drink","strIBA":"Contemporary Classics","strAlcoholic":"Alcoholic","strGlass":"Cocktail glass","strInstructions":"Rub the rim of the glass with the lime slice to make the salt stick to it. Take care to moisten only the outer rim and sprinkle the salt on it. The salt should present to the lips of the imbiber and never mix into the cocktail. Shake the other ingredients with ice, then carefully pour into the glass.","strDrinkThumb":"http:\/\/www.thecocktaildb.com\/images\/media\/drink\/wpxpvu1439905379.jpg","strIngredient1":"Tequila","strIngredient2":"Triple sec","strIngredient3":"Lime juice","strIngredient4":"Salt","strIngredient5":"","strIngredient6":"","strIngredient7":"","strIngredient8":"","strIngredient9":"","strIngredient10":"","strIngredient11":"","strIngredient12":"","strIngredient13":"","strIngredient14":"","strIngredient15":"","strMeasure1":"1 1\/2 oz ","strMeasure2":"1\/2 oz ","strMeasure3":"1 oz ","strMeasure4":"","strMeasure5":"","strMeasure6":"","strMeasure7":"","strMeasure8":"","strMeasure9":"","strMeasure10":"","strMeasure11":"","strMeasure12":"","strMeasure13":"","strMeasure14":"","strMeasure15":"","dateModified":"2015-08-18 14:42:59"}]}

let x = drinks.drinks[0];
result = '';
for (var i=1; i<=15; i++) {
  let var1 = 'strMeasure' + i;
  let var2 = 'strIngredient' + i; 
  result = result + x[var1] + ' ' + x[var2];
}
result = result.trim();