0

I have an array that looks like below:

[
  { "name":"ABC", "group":"A" },
  { "name":"XYZ", "group":"A" },
  { "name":"KLP", "group":"A" },
  { "name":"AKG", "group":"B" },
  { "name":"DIS", "group":"B" },
  { "name":"FAC", "group":"B" },
  { "name":"TAM", "group":"B" },
  { "name":"NEW", "group":"C" },
  { "name":"UTL", "group":"C" },
  { "name":"WAC", "group":"C" }
]

How can I sort this by group in angular I want result like below

{
  "A":[ "ABC", "XYZ", "KLP" ],
  "B":[ "AKG", "DIS", "FAC", "TAM" ],
  "C":[ "NEW", "UTL", "WAC" ]
}

Please help me to do so Thanks in advance

1 Answer 1

3
const sortedData: any = {};
for (const d of this.data) {
    sortedData[d.group] ? sortedData[d.group].push(d.name) : sortedData[d.group] = [d.name];
}
Sign up to request clarification or add additional context in comments.

1 Comment

thannks. it is working

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.