Friday 24 August 2018

How to Create Dynamic associative array in typescript?

Hello



public data:Array<any> = [
{"id":10, "text": "First"},
{"id":11, "text": "Second"},
{"id":12, "text": "Third"},
{"id":10, "text": "Fourth"},
{"id":11, "text": "Fifth"},
{"id":13, "text": "Sixth"}
];
public array: any[][] = [];

for(let i: number=0;i<this.data.length;i++){
this.array[this.data[i].id] = this.array[this.data[i].id] || [];
this.array[this.data[i].id].push(this.data[i].text);
}

this.array.forEach(element => {
console.log(element);
});