2016-05-02 48 views
0

我有一個數組包含JSON數組中的多個元素, 我可以通過做category.language來訪問(JSON中還有其他幾個可訪問的元素例如做category.namecategory.photo等)Angular2/Ionic - 查找數組中的重複項並對它們進行組織

English, Russian, French, Russian, English 

我想要做的就是把重複的相互和最終達到有:

English, Russian, French 

不去除DUPLIC ates,因爲刪除它們也會刪除其他相關元素,例如category.name ...,這些元素具有其他不同的值。

目前我獲取形式的REST API,下面是我的代碼:

Page.js

this.menuPages = { 
      pagination: '.swiper-pagination', 
      //need to have 1 or more (it should some how dynamic depending on the number of the unique items, i.e. sometimes should 1 slide per view, sometimes 10 slides per view) 
      slidesPerView: '1', 
      paginationClickable: true, 
      paginationBulletRender: function (index, className) { 
      console.log('print now2 : '+storeCategory[index]); 
      return '<span class="' + className + '">' + storeCategory[index] + '</span>'; 
      } 
     } 

page.html中

<ion-slides pager [options]="Pages"> 
      <ion-slide *ngFor="#category of categories"> 
    //put the duplicates with each other, to have one element or more in every slide(page) 
      <ion-row> 
      <ion-col> 
      <div class="title">{{category.name}</div> 
      <div class="subTitle"><br>{{category.photo}}</div> 
      </ion-col> 
      </ion-row> 
      </ion-slide> 
    </ion-slides> 

我相信玩耍或者通過page.html或者page.js應該解決問題!我希望你能提出一些想法來解決它!

回答

1

我不知道我得到你的要求,但這裏是基於我所瞭解的一些代碼:

var pages = {}; // create object to hold the collections 
categories.map(cat=>{ 
     if (!pages[cat.language]) 
      pages[cat.language]=[]; // create a list for every language 
     return cat; 
    }) 
    .forEach(cat=>pages.push(cat)); // push every category to the related list 

Pages對象將命名爲每一種語言特性:

pages = { 
     English:[ 
      {language:'English',name:'...',photo:'...'}, 
      {language:'English',name:'...',photo:'...'} 
     ], 
     Russian:[ 
      {language:'Russian',name:'...',photo:'...'}, 
      {language:'Russian',name:'...',photo:'...'} 
     ] 
    } 
相關問題