删除/过滤重复数组 - 角度

时间:2018-05-22 10:10:51

标签: javascript angular ecmascript-6

我有一个角度6项目,我试图从数组中删除重复的条目。

我在这里读过很多关于使用过滤器,设置,传播操作符等的帖子,但这些帖子中的答案并没有过滤掉重复的内容。

然而,这些方法中的每一个似乎都没有删除重复项。

感谢任何关于为什么会出现这种情况的指导,我一定不知道这些方法正在做些什么。

已尝试的方法

ES6 SET方法

this.newBoardMembers = [].concat.apply([], this.newBoardMembers);
      console.log('Consolidated Members: ', this.newBoardMembers);

      this.unique_array = Array.from(new Set(this.newBoardMembers));
      console.log('Unique Array: ', this.unique_array);

过滤方法

this.newBoardMembers = [].concat.apply([], this.newBoardMembers);
      console.log('Consolidated Members: ', this.newBoardMembers);

      this.unique_array = this.newBoardMembers.filter(function(elem, index, self) {
        return index === self.indexOf(elem);
      });
      console.log('Unique Array: ', this.unique_array);

结果

在过滤或设置方法之前

[ { "id": "NI2eyJAZBFYlEE7F5WOfqXpF9ky1", "name": "craig.cocker@mibusiness.com", "avatar": "https://www.listchallenges.com/f/items/83a48b8e-d8ba-4381-9f6b-a239aa0e50f2.jpg" }, { "id": "fL3KY51ooyUc4mP3mobYix1uTJG2", "name": "craig.cocker@bytes.ae", "avatar": "https://cdn.famousoutfits.com/wp-content/uploads/2015/01/matt-bomer.png" }, { "id": "rWdVaf9lN7dl5rdAlZSh", "name": "nevillerob@me.com", "avatar": "https://image.tmdb.org/t/p/original/hErUwonrQgY5Y7RfxOfv8Fq11MB.jpg" }, { "id": "zVUtlx45vKgwcGDaaALaPT36x5f2", "name": "craig.cocker@gmail.com", "avatar": "https://i.pinimg.com/736x/28/0b/81/280b81785d7ba3ae8f993a94866021cf--william-levi-profile-pics.jpg" }, { "id": "NI2eyJAZBFYlEE7F5WOfqXpF9ky1", "name": "craig.cocker@mibusiness.com", "avatar": "https://www.listchallenges.com/f/items/83a48b8e-d8ba-4381-9f6b-a239aa0e50f2.jpg" }, { "id": "fL3KY51ooyUc4mP3mobYix1uTJG2", "name": "craig.cocker@bytes.ae", "avatar": "https://cdn.famousoutfits.com/wp-content/uploads/2015/01/matt-bomer.png" }, { "id": "rWdVaf9lN7dl5rdAlZSh", "name": "nevillerob@me.com", "avatar": "https://image.tmdb.org/t/p/original/hErUwonrQgY5Y7RfxOfv8Fq11MB.jpg" }, { "id": "zVUtlx45vKgwcGDaaALaPT36x5f2", "name": "craig.cocker@gmail.com", "avatar": "https://i.pinimg.com/736x/28/0b/81/280b81785d7ba3ae8f993a94866021cf--william-levi-profile-pics.jpg" }, { "avatar": "https://www.listchallenges.com/f/items/83a48b8e-d8ba-4381-9f6b-a239aa0e50f2.jpg", "id": "NI2eyJAZBFYlEE7F5WOfqXpF9ky1", "name": "craig.cocker@mibusiness.com" }, { "avatar": "https://cdn.famousoutfits.com/wp-content/uploads/2015/01/matt-bomer.png", "id": "fL3KY51ooyUc4mP3mobYix1uTJG2", "name": "craig.cocker@bytes.ae" }, { "avatar": "https://image.tmdb.org/t/p/original/hErUwonrQgY5Y7RfxOfv8Fq11MB.jpg", "id": "rWdVaf9lN7dl5rdAlZSh", "name": "nevillerob@me.com" }, { "avatar": "https://i.pinimg.com/736x/28/0b/81/280b81785d7ba3ae8f993a94866021cf--william-levi-profile-pics.jpg", "id": "zVUtlx45vKgwcGDaaALaPT36x5f2", "name": "craig.cocker@gmail.com" }, { "avatar": "assets/images/avatars/alice.jpg", "id": "56027c1930450d8bf7b10758", "name": "Alice Freeman" }, { "avatar": "assets/images/avatars/danielle.jpg", "id": "26027s1930450d8bf7b10828", "name": "Danielle Obrien" }, { "avatar": "assets/images/avatars/james.jpg", "id": "76027g1930450d8bf7b10958", "name": "James Lewis" }, { "avatar": "assets/images/avatars/vincent.jpg", "id": "36027j1930450d8bf7b10158", "name": "Vincent Munoz" } ]

过滤/设置方法

[ { "id": "NI2eyJAZBFYlEE7F5WOfqXpF9ky1", "name": "craig.cocker@mibusiness.com", "avatar": "https://www.listchallenges.com/f/items/83a48b8e-d8ba-4381-9f6b-a239aa0e50f2.jpg" }, { "id": "fL3KY51ooyUc4mP3mobYix1uTJG2", "name": "craig.cocker@bytes.ae", "avatar": "https://cdn.famousoutfits.com/wp-content/uploads/2015/01/matt-bomer.png" }, { "id": "rWdVaf9lN7dl5rdAlZSh", "name": "nevillerob@me.com", "avatar": "https://image.tmdb.org/t/p/original/hErUwonrQgY5Y7RfxOfv8Fq11MB.jpg" }, { "id": "zVUtlx45vKgwcGDaaALaPT36x5f2", "name": "craig.cocker@gmail.com", "avatar": "https://i.pinimg.com/736x/28/0b/81/280b81785d7ba3ae8f993a94866021cf--william-levi-profile-pics.jpg" }, { "id": "NI2eyJAZBFYlEE7F5WOfqXpF9ky1", "name": "craig.cocker@mibusiness.com", "avatar": "https://www.listchallenges.com/f/items/83a48b8e-d8ba-4381-9f6b-a239aa0e50f2.jpg" }, { "id": "fL3KY51ooyUc4mP3mobYix1uTJG2", "name": "craig.cocker@bytes.ae", "avatar": "https://cdn.famousoutfits.com/wp-content/uploads/2015/01/matt-bomer.png" }, { "id": "rWdVaf9lN7dl5rdAlZSh", "name": "nevillerob@me.com", "avatar": "https://image.tmdb.org/t/p/original/hErUwonrQgY5Y7RfxOfv8Fq11MB.jpg" }, { "id": "zVUtlx45vKgwcGDaaALaPT36x5f2", "name": "craig.cocker@gmail.com", "avatar": "https://i.pinimg.com/736x/28/0b/81/280b81785d7ba3ae8f993a94866021cf--william-levi-profile-pics.jpg" }, { "avatar": "https://www.listchallenges.com/f/items/83a48b8e-d8ba-4381-9f6b-a239aa0e50f2.jpg", "id": "NI2eyJAZBFYlEE7F5WOfqXpF9ky1", "name": "craig.cocker@mibusiness.com" }, { "avatar": "https://cdn.famousoutfits.com/wp-content/uploads/2015/01/matt-bomer.png", "id": "fL3KY51ooyUc4mP3mobYix1uTJG2", "name": "craig.cocker@bytes.ae" }, { "avatar": "https://image.tmdb.org/t/p/original/hErUwonrQgY5Y7RfxOfv8Fq11MB.jpg", "id": "rWdVaf9lN7dl5rdAlZSh", "name": "nevillerob@me.com" }, { "avatar": "https://i.pinimg.com/736x/28/0b/81/280b81785d7ba3ae8f993a94866021cf--william-levi-profile-pics.jpg", "id": "zVUtlx45vKgwcGDaaALaPT36x5f2", "name": "craig.cocker@gmail.com" }, { "avatar": "assets/images/avatars/alice.jpg", "id": "56027c1930450d8bf7b10758", "name": "Alice Freeman" }, { "avatar": "assets/images/avatars/danielle.jpg", "id": "26027s1930450d8bf7b10828", "name": "Danielle Obrien" }, { "avatar": "assets/images/avatars/james.jpg", "id": "76027g1930450d8bf7b10958", "name": "James Lewis" }, { "avatar": "assets/images/avatars/vincent.jpg", "id": "36027j1930450d8bf7b10158", "name": "Vincent Munoz" } ]

0 个答案:

没有答案