Issue
This is how my array looks like: and my array is not type of string, it's type of it own object called MyObject (just like setter and getter objects in java)
["Car","model","year","color","price"]
["Table","model","year","color","price"]
["Car","model","year","color","price"]
["Car","model","year","color","price"]
["Laptop","model","year","color","price"]
["Laptop","model","year","color","price"]
now I want to group by this in typescript and count how many of that item exist in array (also like in sql)
name |count
Car | 3
Laptop| 2
Table | 1
and in my typescript file I have this
private groupByObjects() {
//this read all data in array allData[] from service
this.dataService.retrieveData().subscribe(allData => {
});
}
Could anyone please help me to write this in typescript?
Solution
I tried a lot and can write the answer which is works for me perfectly. So I post it now and hope it helps anyone who has the same problem as mine.
private groupByElement(receivedData: ReceivedData, elements: Array<GroupByElement>) {
let groupElement: GroupByElement = new GroupByElement;
if (!elements.find(x => x.element== receivedData.element)) {
groupElement.element= receivedData.element;
groupElement.count = 1;
elements.push(groupElement);
} else {
this.updateElementCounter = elements.find(x => x.element== receivedData.element)?.count;
this.updateElementCounter! += 1;
this.indexElementCount = elements.findIndex(x => x.element== receivedData.element);
elements[this.indexElementCount].count = this.updateElementCounter!;
}
}
Answered By - SpacemanSps
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.