1

I want to group the data based on the type and type_id Here is the array

var addArray = [
                    {
                        "id": 24,
                        "language_id": 3,
                        "type": "service",
                        "type_id": 2,
                        "key": "service seeker",
                        "value": " need service"
                    },
                    {
                        "id": 23,
                        "language_id": 3,
                        "type": "service",
                        "type_id": 2,
                        "key": "phone",
                        "value": "phone number"
                    },
                    {
                        "id": 24,
                        "language_id": 3,
                        "type": "service",
                        "type_id": 7,
                        "key": "tester",
                        "value": "service tester"
                    }
                    {
                        "id": 19,
                        "language_id": 3,
                        "type": "offer",
                        "type_id": 4,
                        "key": "source",
                        "value": "resource"
                    }
                ]

I have tried let result = _.groupBy(addArray,'type') it is grouping the data based on type but I need to group by type as well as type_id

Expected output

1 Answer 1

1

If you need the a flat grouping based on two or more properties, use the _.groupBy() callback to combine the properties to a string:

const addArray = [{"id":24,"language_id":3,"type":"service","type_id":2,"key":"service seeker","value":" need service"},{"id":23,"language_id":3,"type":"service","type_id":2,"key":"phone","value":"phone number"},{"id":24,"language_id":3,"type":"service","type_id":7,"key":"tester","value":"service tester"},{"id":19,"language_id":3,"type":"offer","type_id":4,"key":"source","value":"resource"}]

const result = _.groupBy(addArray, o => `${o.type}-${o.type_id}`)

console.log(result)
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.min.js"></script>

If you need a multi level grouping, start by grouping by the type, then map the groups with _.values(), and group them again by type_id:

const { flow, partialRight: pr, groupBy, mapValues } = _

const fn = flow(
  pr(groupBy, 'type'),
  pr(mapValues, g => groupBy(g, 'type_id'))
)

const addArray = [{"id":24,"language_id":3,"type":"service","type_id":2,"key":"service seeker","value":" need service"},{"id":23,"language_id":3,"type":"service","type_id":2,"key":"phone","value":"phone number"},{"id":24,"language_id":3,"type":"service","type_id":7,"key":"tester","value":"service tester"},{"id":19,"language_id":3,"type":"offer","type_id":4,"key":"source","value":"resource"}]

const result = fn(addArray)

console.log(result)
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.min.js"></script>

Sign up to request clarification or add additional context in comments.

Comments

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.