我的目标是列出产品的所有成分。不幸的是,有多个成分阵列,每个阵列中都有多种成分。阵列的数量会因产品而异,因此我需要一定程度地捕获所有成分。到目前为止,我有:
查找所有成分数组并单独列出
hashcode
列出单个定义成分数组的成分,在本例中为数组2.
$(function() {
var params = {
// Request parameters
// "gtin": "{string}",
// "tpnb": "{string}",
// "tpnc": "{string}",
// "catid": "{string}",
// "gtin": "05052004435789",
"tpnc": "285363525",
};
$.ajax({
url: "https://dev.tescolabs.com/product/?" + $.param(params),
beforeSend: function(xhrObj){
// Request headers
xhrObj.setRequestHeader("Ocp-Apim-Subscription-Key","key");
},
type: "GET",
// Request body
data: "{body}",
})
.done(function(data) {
// alert("success");
// alert(data);
console.log(data);
var ingredientArrays = data.products[0].ingredients;
var l = ingredientArrays.length;
for (i = 0; i < l; i++){
var allIngredients = ingredientArrays[i];
console.log(allIngredients);
}
})
.fail(function() {
alert("error");
});
});
到目前为止一切顺利,但我希望合并此功能,以便我可以找到所有数组中的所有成分并创建一个列表。
如果我var list = data.products[0].ingredients[2];
var ingredient = list.split(/[:;,.]+/);
for (list = 0; list < ingredient.length; ++list) {
console.log(ingredient[list]);
}
我得到:
console.log(data);
更新
为了澄清,我可以在控制台日志中返回结果,如下所示:
{
"products": [{
"gtin": "05054402006097",
"tpnb": "073706172",
"tpnc": "285363525",
"description": "Tesco Stonebaked Thin Double Pepperoni Pizza 330G",
"brand": "TESCO",
"qtyContents": {
"quantity": 330.0,
"totalQuantity": 330.0,
"quantityUom": "g",
"netContents": "330g e"
},
"productCharacteristics": {
"isFood": true,
"isDrink": false,
"healthScore": 48,
"isHazardous": false,
"storageType": "Frozen"
},
"ingredients": [
"<strong>Wheat</strong> Flour",
"Tomato Purée",
"Mozzarella Cheese (<strong>Milk</strong>) (16%), Pepperoni (10%), Water, Mini Pepperoni (3.5%), Yeast, Dextrose, Rapeseed Oil, Salt, Sugar, Dried Garlic, Dried Herbs, Spice. Pepperoni contains: Pork, Pork Fat, Salt, Dextrose, Spices, Spice Extracts, Antioxidants (Extracts of Rosemary, Sodium Ascorbate), Preservative (Sodium Nitrite). Mini Pepperoni contains: Pork, Pork Fat, Salt, Dextrose, Spices, Spice Extracts, Sugar, Antioxidants (Sodium Erythorbate, Extracts of Rosemary), Preservative (Sodium Nitrite)."
],
使用此代码:
tesco.js:70
["<strong>Wheat</strong> Flour"]
0
:
"<strong>Wheat</strong> Flour"
length
:
1
__proto__
:
Array(0)
tesco.js:70
["Tomato Purée"]
0
:
"Tomato Purée"
length
:
1
__proto__
:
Array(0)
tesco.js:70
(35) ["Mozzarella Cheese (<strong>Milk</strong>) (16%)", " Pepperoni (10%)", " Water", " Mini Pepperoni (3", "5%)", " Yeast", " Dextrose", " Rapeseed Oil", " Salt", " Sugar", " Dried Garlic", " Dried Herbs", " Spice", " Pepperoni contains", " Pork", " Pork Fat", " Salt", " Dextrose", " Spices", " Spice Extracts", " Antioxidants (Extracts of Rosemary", " Sodium Ascorbate)", " Preservative (Sodium Nitrite)", " Mini Pepperoni contains", " Pork", " Pork Fat", " Salt", " Dextrose", " Spices", " Spice Extracts", " Sugar", " Antioxidants (Sodium Erythorbate", " Extracts of Rosemary)", " Preservative (Sodium Nitrite)", ""]
0
:
"Mozzarella Cheese (<strong>Milk</strong>) (16%)"
1
:
" Pepperoni (10%)"
2
:
" Water"
3
:
" Mini Pepperoni (3"
4
:
"5%)"
5
:
" Yeast"
6
:
" Dextrose"
7
:
" Rapeseed Oil"
8
:
" Salt"
/// and so on\
但是你可以看到它分别计算成分。我希望他们成为一个完整的清单。计数应该从0开始然后上升。而不是根据每个成分数组显示结果,我得到上面的1,1,35的结果。我会得到37的结果。
答案 0 :(得分:1)
因此,您的实际问题是,您希望迭代从服务器收到的response
,并且response
可以有多个products
并且在每个products
内有ingredients
数组,该数组的大小可能会有所不同,您希望能够迭代其中的所有索引。
您应该使用for in
和for
循环。要查看差异,请参阅here
。
我将使用提供的响应数据并对其进行迭代,它将迭代所有products
及其中的所有ingredients
。
请参阅下面的演示
var response = {
"products": [{
"gtin": "05054402006097",
"tpnb": "073706172",
"tpnc": "285363525",
"description": "Tesco Stonebaked Thin Double Pepperoni Pizza 330G",
"brand": "TESCO",
"qtyContents": {
"quantity": 330.0,
"totalQuantity": 330.0,
"quantityUom": "g",
"netContents": "330g e"
},
"productCharacteristics": {
"isFood": true,
"isDrink": false,
"healthScore": 48,
"isHazardous": false,
"storageType": "Frozen"
},
"ingredients": [
"<strong>Wheat</strong> Flour",
"Tomato Purée",
"Mozzarella Cheese (<strong>Milk</strong>) (16%), Pepperoni (10%), Water, Mini Pepperoni (3.5%), Yeast, Dextrose, Rapeseed Oil, Salt, Sugar, Dried Garlic, Dried Herbs, Spice. Pepperoni contains: Pork, Pork Fat, Salt, Dextrose, Spices, Spice Extracts, Antioxidants (Extracts of Rosemary, Sodium Ascorbate), Preservative (Sodium Nitrite). Mini Pepperoni contains: Pork, Pork Fat, Salt, Dextrose, Spices, Spice Extracts, Sugar, Antioxidants (Sodium Erythorbate, Extracts of Rosemary), Preservative (Sodium Nitrite)."
],
}]
}
//console.log(test.products);
let products = response.products;
for (var data in products) {
let product = products[data];
let ingredients = product.ingredients;
console.log("PRODUCT : " + product.brand);
console.log("=======================");
console.log("INGREDIENTS");
for (var i = 0; i < ingredients.length; i++) {
console.log("--------------" + ingredients[i]);
}
console.log("=======================");
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
&#13;
修改强>
在您更新之后,实际上您正在将ingredients
数组内的每个索引拆分为regex
,即/[:;,.]/
,并且数组的结果索引可能会有所不同,并且对于所有这些索引而言分割后的数组要合并为单个数组,因此需要使用Array.prototype.concat()
见下面的演示
var test = {
"products": [{
"gtin": "05054402006097",
"tpnb": "073706172",
"tpnc": "285363525",
"description": "Tesco Stonebaked Thin Double Pepperoni Pizza 330G",
"brand": "TESCO",
"qtyContents": {
"quantity": 330.0,
"totalQuantity": 330.0,
"quantityUom": "g",
"netContents": "330g e"
},
"productCharacteristics": {
"isFood": true,
"isDrink": false,
"healthScore": 48,
"isHazardous": false,
"storageType": "Frozen"
},
"ingredients": [
"<strong>Wheat</strong> Flour",
"Tomato Purée",
"Mozzarella Cheese (<strong>Milk</strong>) (16%), Pepperoni (10%), Water, Mini Pepperoni (3.5%), Yeast, Dextrose, Rapeseed Oil, Salt, Sugar, Dried Garlic, Dried Herbs, Spice. Pepperoni contains: Pork, Pork Fat, Salt, Dextrose, Spices, Spice Extracts, Antioxidants (Extracts of Rosemary, Sodium Ascorbate), Preservative (Sodium Nitrite). Mini Pepperoni contains: Pork, Pork Fat, Salt, Dextrose, Spices, Spice Extracts, Sugar, Antioxidants (Sodium Erythorbate, Extracts of Rosemary), Preservative (Sodium Nitrite)."
],
}]
}
var ingredientArrays = test.products[0].ingredients;
var l = ingredientArrays.length;
var merged = Array();
for (i = 0; i < l; i++) {
var allIngredients = ingredientArrays[i];
var ingredient = allIngredients.split(/[:;,.]+/);
// merged.push(Array.prototype.concat.apply([], ingredient));
merged = merged.concat(ingredient);
}
console.log(merged);
&#13;