gpt4 book ai didi

javascript - 我的 arrayPizzas 有问题,它永远不会进入第一个 if,问题是什么?我想为重复的列表项添加一个 count++

转载 作者:行者123 更新时间:2023-12-02 21:30:30 25 4
gpt4 key购买 nike

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com