【发布时间】:2018-10-08 13:18:30
【问题描述】:
在 Angular 中:
我有以下 JSON 对象:
$scope.catItems = [
{cat: 'A', desc: 'lorem ipsum', tags: '', name: 'abc', link: 'www.google.com'},
{cat: 'B', desc: 'I am here too', tags: '', name: 'def', link: 'www.google.com'},
{cat: 'C', desc: 'So am I', tags: '', name: 'ghi', link: 'www.google.com'},
{cat: 'D', desc: 'Where is the sky', tags: '', name: 'jkl', link: 'www.google.com'},
{cat: 'A', desc: 'I really don't know', tags: '', name: 'mno', link: 'www.google.com'},
{cat: 'A', desc: 'So do I', tags: '', name: 'pqr', link: 'www.google.com'},
{cat: 'C', desc: 'Tell the next person', tags: '', name: 'stu', link: 'www.google.com'},
{cat: 'B', desc: 'So will I', tags: '', name: 'vwx', link: 'www.google.com'}
];
您可以看到同一只猫是如何在对象中重复两次以上的,或者说某些猫只重复两次甚至更少。
我想看看我们是否可以基于上述对象中的 Cat,将它们解析为如下所示的一个数组,并从生成的列表中删除重复项:
$scopt.cats = [];
for (items in $scope.catItems){
if ($scope.cats.indexOf(item.cat) < 0){
$scope.cats.push(item.cat);
}
}
并将它们显示在下面:
<ul data-ng-repeat="items in cats">
<li>
<p class="search-title">{{items}}</p>
</li>
</ul>
此外,如果可能,在其下方显示每个人的属性。
【问题讨论】:
-
@Stradosphere,想法更接近我正在寻找的东西,但我需要将猫存储在一个数组中,然后一旦猫被存储。它们各自的属性需要在它们下面显示。就像,Cat - A 重复三次,所以它的输出是 (Cat - A ---- name : abc name : mno name: pqr)
标签: javascript html arrays angularjs sorting