Sorry for not giving an example to use but I'm not sure how to create codepen angular 5 examples. Anyways. I want to know if it is possible to use *ngFor to repeat through this array, but only show values once (no duplicates). Check my example image of buttons for how it should look. Is this possible with THIS array? Maybe using pipes or something like that?
-
3create a uniqValues array see stackoverflow.com/questions/1960473/…Eliseo– Eliseo2018-03-23 13:07:08 +00:00Commented Mar 23, 2018 at 13:07
-
2I think you have to create a method that clean your array. Btw for Angular example, I recommend you to use StackblitzZooly– Zooly2018-03-23 13:07:10 +00:00Commented Mar 23, 2018 at 13:07
-
Here an an angular stackblitz template which you can use to create demo: stackblitz.com/edit/angular-zyh18hFAISAL– FAISAL2018-03-23 13:20:41 +00:00Commented Mar 23, 2018 at 13:20
-
Possible duplicate of Get all unique values in an array (remove duplicates)Oliver– Oliver2018-03-23 13:25:08 +00:00Commented Mar 23, 2018 at 13:25
Add a comment
|
1 Answer
A possible solution:
First structure your array:
this.array = [
{level1: "1", level2: "1.1", level3: "1.1.1"},
{level1: "1", level2: "1.1", level3: "1.1.2"},
{level1: "1", level2: "1.2", level3: "1.2.1"}
];
this.fa = {}
this.array.forEach(obj => {
if(!this.fa[obj.level1]) this.fa[obj.level1] = {};
if(!this.fa[obj.level1][obj.level2]) this.fa[obj.level1][obj.level2] = [];
this.fa[obj.level1][obj.level2].push(obj.level3)
})
It assumes you have only 3 levels. Then you will get the following structure:
{
"1": {
"1.1": [
"1.1.1",
"1.1.2"
]
}
}
Then iterate through in template:
<div *ngFor="let i of Object.keys(fa)">
{{i}}
<div *ngFor="let j of Object.keys(fa[i])">
{{j}}
<div *ngFor="let k of fa[i][j]">
{{k}}
</div>
</div>
</div>
Don't forget to bind the Object variable to your template adding the following line in your component:
Object = Object;
Complete example:
@Component({
selector: 'app',
template: `
<div *ngFor="let i of Object.keys(fa)">
{{i}}
<div *ngFor="let j of Object.keys(fa[i])">
{{j}}
<div *ngFor="let k of fa[i][j]">
{{k}}
</div>
</div>
</div>
`,
})
export class AppComponent {
array = [
{level1: "1", level2: "1.1", level3: "1.1.1"},
{level1: "1", level2: "1.1", level3: "1.1.2"},
{level1: "1", level2: "1.2", level3: "1.2.1"}
];
fa = {};
Object = Object;
constructor() {
this.array.forEach(obj => {
if (!this.fa[obj.level1]) this.fa[obj.level1] = {};
if (!this.fa[obj.level1][obj.level2]) this.fa[obj.level1][obj.level2] = [];
this.fa[obj.level1][obj.level2].push(obj.level3)
})
}
}
5 Comments
Frank
Thank you for the answer. It seems much shorter than the other answers, but I can't seem to get it working. I think there is something wrong in the html template? I don't understand the Object.keys thing
Alexandre Annic
My bad, you must add Object=Object in the composenent to makes it accessible in the template.
Frank
Say again? Sorry I don't understand 100%. Should I add "Object = Object;" in the component? Would you update your example please? I prefer your answer but I can't get it to work
Alexandre Annic
To make a javascript global variable (as
Object) accessible to your template, you must bind it. So in your component you must add the following line Object = Object inside the Typescript code of your component like this: @Component({...}) export class ... { ... Object = Object; ... }. I will add a complete compilable example.Frank
yes it works man! I can't exactly remember how I finally used it though. But I will mark yours. Thank you very much

