我有一个数组,其中包含 JSON 数组中的多个元素,
我可以通过执行 category.language
来访问执行操作(JSON 中还有一些其他元素可以访问,例如执行 category.name
、category.photo
等)
English, Russian, French, Russian, English
我想做的是将重复项放在一起,最终得到:
English, Russian, French
不删除重复项,因为删除它们也会删除其他相关元素,例如具有其他不同值的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>';
}
}
页面.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
应该可以解决这个问题!我希望你能提出一些解决这个问题的想法!
最佳答案
我不清楚你到底在问什么,但你可以使用 ES6 集来确保每个条目的唯一性。
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set
关于javascript - Angular2/Ionic - 查找数组中的重复项并组织它们,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36993427/