作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
this.arrayPizzas = [
0: {
name: "4 Stagioni",
price: 13,
ingredients: "250 g of pizza dough, 150 g of tomato sauce, 30 g …ck and green olives, 30 g of champignon mushrooms",
image: "../assets/img/pizza-4stag.png"
},
1: {
name: "Margherita",
price: 10,
ingredients: "Manitoba flour 200 g, Water 300 ml, Salt up to 10 g, Extra virgin olive oil 35 g, Tomato pulp 500 g",
image: "../assets/img/pizza-margherita.png"
},
2: {
name: "Margherita",
price: 10,
ingredients: "Manitoba flour 200 g, Water 300 ml, Salt up to 10 g, Extra virgin olive oil 35 g, Tomato pulp 500 g",
image: "../assets/img/pizza-margherita.png"
},
3: {
name: "Margherita",
price: 10,
ingredients: "Manitoba flour 200 g, Water 300 ml, Salt up to 10 g, Extra virgin olive oil 35 g, Tomato pulp 500 g",
image: "../assets/img/pizza-margherita.png"
},
4: {
name: "Vegetarian",
price: 13.5,
ingredients: "400 g of pizza dough, 300 g of tomato sauce, 1 eggplant, 2 peppers, 1 courgette",
image: "../assets/img/pizza-vegetariana.png"
},
5: {
name: "Vegetarian",
price: 13.5,
ingredients: "400 g of pizza dough, 300 g of}];
this.arrayPizzas = this.local.sort((a, b) => (a.name > b.name) ? 1 : -1);
this.arrayPizzas.forEach(element => {
const count = 0;
if (this.newArray.includes(element.name)) {
console.log('include');
this.newArray[element].counter = element.counter + 1;
} else if (!this.newArray.includes(element.name)) {
console.log('else');
const a: Cart = {
name: element.name,
counter: count,
price: element.price
};
this.newArray.push(a);
}
});
最佳答案
这对你有用并计算披萨名称的出现次数
arrayPizzas = [{
name: "Margherita",
price: 10,
ingredients: "Manitoba flour 200 g, Water 300 ml, Salt up to 10 g, Extra virgin olive oil 35 g, Tomato pulp 500 g",
image: "../assets/img/pizza-margherita.png"
},
{
name: "Vegetarian",
price: 13.5,
ingredients: "400 g of pizza dough, 300 g of tomato sauce, 1 eggplant, 2 peppers, 1 courgette",
image: "../assets/img/pizza-vegetariana.png"
},
{
name: "Vegetarian",
price: 13.5,
ingredients: "400 g of pizza dough, 300 g of"
}
]
newArray = [];
this.arrayPizzas.forEach((element) => {
const index = this.newArray.findIndex(x => x.name === element.name)
if (index > -1) {
this.newArray[index].counter += 1
} else {
const a = {
name: element.name,
counter: 1,
price: element.price
};
this.newArray.push(a);
}
});
console.log(newArray)
关于javascript - 我的 arrayPizzas 有问题,它永远不会进入第一个 if,问题是什么?我想为重复的列表项添加一个 count++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60634665/
关闭。此题需要details or clarity 。目前不接受答案。 想要改进这个问题吗?通过 editing this post 添加详细信息并澄清问题. 已关闭 3 年前。 Improve th
我是一名优秀的程序员,十分优秀!