javascript - Typescript - 无法拼接数组

标签 javascript angular typescript

当未选中复选框时(如果复选框值与数组项中的值匹配),我尝试删除数组的一部分。当用户单击复选框时创建的数组工作正常,但是当取消选中复选框时,我尝试删除/拼接的数组对象现在是一个字符串对象,因此无法拼接。我的困惑在于为什么数组在第二次循环后“变成”字符串对象。

我正在努力处理的主要代码块始于:onCheckboxChange(option, event)

Stackblitz 在这里:https://stackblitz.com/edit/angular-match-groceries-sweets

import { Component } from '@angular/core';
// import * as d3 from "d3";
@Component({
  selector: 'my-app',
  templateUrl: './app.component.html',
  styleUrls: [ './app.component.css' ]
})
export class AppComponent  {
  name = 'Angular';
  grocery_list: any;
  treats: [];
  box: any;
  allTreats: any;
  noDupes: any;
  selectedAll: any;
  isChecked: boolean;
  showTreats: any;
  checkedList: string[] = [];

constructor(){}

ngOnInit(){
  this.getTreats();
}
  getTreats(){
  this.grocery_list = [

      {"fruit": "apple", "veggie": "lettuce", "sweets": ["cookies", "cake"]},
      {"fruit": "orange", "veggie": "asparagus", "sweets": ["cookies", "ice cream"]},
      {"fruit": "apple", "veggie": "carrots", "sweets": ["No Data", "cake"]},
      {"fruit": "apple", "veggie": "lettuce", "sweets": ["cookies", "No Data"]},
    ];

  this.treats = this.grocery_list.map(x => x.sweets); 
  this.box = [].concat.apply([],this.treats);
  this.noDupes = Array.from(new Set(this.box));
  this.allTreats = Array.from(this.box)
  }

// this is where the check/uncheck logic starts
    onCheckboxChange(option, event) {
      let eChecked = event.target.checked;
     if(eChecked) {
       this.grocery_list.forEach(x => {
         x.sweets.forEach(y => {
           if (y === option){
             this.checkedList.push(x);
           }
         });
       });
// this is the part that isn't working
     } else {
       this.checkedList.forEach(c, i => {
         c.sweets.forEach(k => {
           // if 'sweet' name = option, remove its array object
           if (k === option) {
             this.checkedList.splice(i, this.checkedList.length)
           }
         })

         }) 
     }
     }
}

最佳答案

请找到更简单的方法来解决问题

 onCheckboxChange(option, event) {
  let checkedState = event.target.checked;
  if(checkedState === true)
    this.checkedList = this.checkedList.concat(this.grocery_list.filter(item => item.sweets.includes(option)))
  else
    this.checkedList = this.checkedList.filter(item => !item.sweets.includes(option));
    // the above code checks for items which contains sweets array with selected option and keeps only those which doesn't
  }

更新 更改以下类型

checkedList: any[] = []; // or create an interface based on your grocery_list

关于javascript - Typescript - 无法拼接数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58274205/

相关文章:

javascript - 了解 jQuery slider 函数

google-maps - 如何在 angular2 中显示带有地址的谷歌地图

javascript - Angular 2 SPA 加载屏幕未显示 Visual Studio 2017

typescript - 类型 'moveFile' ionic typescript 上不存在属性 'typeof File'

javascript - 在javascript中从文件中读取json

javascript - 用什么代替 JavaScript 中的类?

javascript - 主题不发出数据

css - 复选框警报 Controller - 添加 3 个按钮

reactjs - 我们需要带有 TypeScript 的 propTypes 吗?

javascript - 如何通过 JavaScript 获取媒体查询定义的当前 CSS 值?