简体   繁体   English

在没有管道的情况下过滤Angular4中的对象数组

[英]Filter array of objects in Angular4 without pipe

I have an array of links that each element is an object that contain few strings - a link, description and category. 我有一个链接数组,每个元素都是一个包含几个字符串的对象-链接,描述和类别。 I have different components that display the links, and I want in each component to display only the links of its category. 我有显示链接的不同组件,并且我希望在每个组件中仅显示其类别的链接。 So I want to filter the array by the category. 所以我想按类别过滤数组。

I have a mock-up array with all the links. 我有一个包含所有链接的模型数组。

I try to filter the array of objects without a pipe. 我尝试过滤没有管道的对象数组。 The reason why: https://angular.io/guide/pipes#appendix-no-filterpipe-or-orderbypipe 原因如下: https : //angular.io/guide/pipes#appendix-no-filterpipe-or-orderbypipe

Apparently the Angular team suggests to do the filtering in the component level and not using a pipe: "The Angular team and many experienced Angular developers strongly recommend moving filtering and sorting logic into the component itself." 显然,Angular团队建议在组件级别进行过滤,而不要使用管道:“ Angular团队和许多经验丰富的Angular开发人员强烈建议将过滤和排序逻辑移至组件本身。”

So here's my component: 所以这是我的组件:

@Component({
    selector: 'startups',
    templateUrl: './startups.component.html'
})

export class StartupsComponent implements OnInit {

constructor(private appLinksService: DigitalCoinHubService) { }

title = 'Startups';


links: DCHlinks[]; // create a new array in type of DCHlinks to get the data

startupsLinks: DCHlinks [] = []; // will build the startsups links only 


getLinks(): void {
  this.links = this.appLinksService.getLinks(); // gets the array with the data from the service

  for (let i in this.links)
  {
     if (this.links[i].dchCategory == 'startups' )
     {
         this.startupsLinks[i].push(this.links[i]);
     }

  }

}

ngOnInit() {
    this.getLinks();   

}

} }

So first I get the big array from the service: 因此,首先我要从服务中获取大数组:

this.links = this.appLinksService.getLinks();

And then I try to build a new array that will contain only the relevant links. 然后,我尝试构建一个仅包含相关链接的新数组。 The filter is by the category. 过滤器是按类别分类的。 But then when I try to build the new array by push the elements which their category matches - it gives me error: 但是然后当我尝试通过推入其类别匹配的元素来构建新数组时,它给了我错误:

Property 'push' does not exist on type 'DCHlinks'. 类型“ DCHlinks”上不存在属性“ push”。

DCHlinks is the object - this is the class: DCHlinks是对象-这是类:

export class DCHlinks {
   dchLink: string;
   dchLinkTitle: string;
   dchLinkDescription: string;
   dchCategory: string;
 }

Any idea how to do this simple filter? 任何想法如何做这个简单的过滤器? (and w/o pipe - see above reason why..) (和不带管道-参见上述原因。)

Thanks! 谢谢!

You need to intialize the array as you did for startupsLinks 您需要像startupsLinks一样初始化数组

links: DCHlinks[] = [];

Or you can simply use array.filter to get the relavant data 或者,您可以简单地使用array.filter获取相关数据

this.startupsLinks = this.links.filter(t=>t.dchCategory == 'startups');

I have the same issue but I resolve in different way. 我有相同的问题,但是我以不同的方式解决。 I have the Array of object and want to use filtering using of html select option .which I given the data that filter the array of object. 我有对象数组,并想使用html select选项的过滤。我给了过滤对象数组的数据。

`$`





  
 
  
  
    heroes = [{
        name: "shubh",
        franchise: "eng"
      },
      {
        name: "Ironman",
        franchise: "Marvel"
      },
      {
        name: "Batman",
        franchise: "DC"
      },
      {
        name: "Batman",
        franchise: "DC"
      },
      {
        name: "Batman",
        franchise: "DC"
      },
      {
        name: "satman",
        franchise: "mc"
      },
      {
        name: "monmam",
        franchise: "DC"
      },
      {
        name: "loolman",
        franchise: "DC"
      },
      {
        name: "Thor",
        franchise: "Marvel"
      },
      {
        name: "monmam",
        franchise: "DC"
      },
      {
        name: "monmam",
        franchise: "DC"
      },
      {
        name: "monmam",
        franchise: "DC"
      },

      {
        name: "Thor",
        franchise: "Marvel"
      },
      {
        name: "Superman",
        franchise: "DC"
      },
      {
        name: "Superman",
        franchise: "DC"
      },
      {
        name: "Superman",
        franchise: "DC"
      },
      {
        name: "Superman",
        franchise: "DC"
      },

    ];
    //this is the most imp part in the filter section .I face lot of problem this is not working if this line not write .The filter method works old one time.
    newarr = this.heroes;

    //So I create new array which store the old array value every  time. and we replace the value in the filter function.
    filter(heroes: string) {
      console.log(heroes);
      this.heroes = this.newarr; // replace the value and store old value
      let heroesnew = this.heroes.filter((data => data.name == heroes));

      this.heroes = heroesnew;
      console.log(this.heroes);

    }
    <!––"#sel" is the template variable which gives the data of value property in option field ––>
    <select #sel class="custom-select custom-select-sm" (change)="filter(sel.value)">
      <option>All</option>
      <option value="Batman">Batman</option>
      <option value="Superman">Superman</option>
      <option value="satman">satman</option>
      <option value="monmam">monmam</option>
      <option value="Thor">thor</option>
    </select>

    <!–– this is the table that I want to filter––>
   <div>
      <table class="table table-hover ">
        <thead class="thead-dark">
          <tr>
            <th>#</th>
            <th>name</th>
            <th>franchise</th>
          </tr>
        </thead>
        <tbody>
          <tr *ngFor="let incidence of heroes">
            <td>{{ incidence.name}}</td>
            <td> {{ incidence.franchise }}</td>
          </tr>
        </tbody>
      </table>
    </div>

$

use the code for angular 2+,to 8 It working fine .. 使用代码的角度2+,到8它工作正常..

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM