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

标签 javascript angular typescript

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/

相关文章:

javascript - 条形图和折线图在 dc.js 中不交互

typescript - 使用 'bun dev' 进行热重载 (HMR)

Angular 5 ng-bootstrap datepicker 禁用数组中的特定日期

node.js - angular - node.js,HttpClient 连接成功但没有成功

javascript - CKeditor5 + Angular : how to display video added via editor

typescript - 在 TypeScript 中以编程方式向具有类型安全性的类添加方法?

typescript - TypeScript 中的 StringBuilder 等价物是什么?

javascript - 解释/解析来自蓝牙心率监测器(Cordova)的数据

javascript - 隐藏输入按钮

javascript - 级联下拉菜单编辑在 Angular 中不起作用?