Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Grouping returns two values instead one #10

Open
codewithelvin opened this issue Mar 22, 2019 · 1 comment
Open

Grouping returns two values instead one #10

codewithelvin opened this issue Mar 22, 2019 · 1 comment

Comments

@codewithelvin
Copy link

I am trying to group in for loop using your extension. When I am trying to group by skillcategory it shows two times instead once.

Here is the data i get:

[
  {
    "0": {
      "skillid": 1,
      "skillname": "PHP",
      "skillcategory": "Programing Langugages",
      "skilllevel": 62
    },
    "4": {
      "skillid": 5,
      "skillname": "C#",
      "skillcategory": "Programing Langugages",
      "skilllevel": 50
    }
  },
  {
    "1": {
      "skillid": 2,
      "skillname": "Walking",
      "skillcategory": Others,
      "skilllevel": 20
    },
    "2": {
      "skillid": 3,
      "skillname": "Jumping",
      "skillcategory": Others,
      "skilllevel": 100
    },
    "3": {
      "skillid": 4,
      "skillname": "Naping",
      "skillcategory": Others,
      "skilllevel": 80
    }
  }
]

What I actually want to get

Programing Langugages
PHP, C#

Others
Walking, Jumping, Naping

And it is my version of twig code:

{% for key, i in skills|group_by(=> _.skillcategory) %}
   {% for b in i %}
       <div>{{b.skillcategory}}</div>
       {{b.skillname}}
   {% endfor %}
{% endfor %}
@dpolac
Copy link
Owner

dpolac commented Mar 22, 2019

Hi @neo-azazello
You should put category name outside of the second for. Like this:

{% for key, i in skills|group_by(=> _.skillcategory) %}
   <div>{{key}}</div>
   {% for b in i %}
       {{b.skillname}}, 
   {% endfor %}
{% endfor %}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants