2016-05-02 45 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:'...'} 
     ] 
    } 
相关问题