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);
});

2 comments:


  1. Good day. I was impressed with your article. Keep it up . You can also visit my site if you have time. Thank you and Bless you always.
    Outsource Angularjs Application Development in India

    ReplyDelete