-2

I would like to format:

[
    {
        "id": 1,
        "location": {
            "id": 1,
            "title": "location 1"
        },
    },
    {
        "id": 2,
        "location": {
            "id": 1,
            "title": "location 1"
        },
    },
    {
        "id": 3,
        "location": {
            "id": 2,
            "title": "location 2"
        },
    }
]

To:

[
    {
        "id": 1,
        "title": "location 1",
        "items": [
            {
                "id": 1
            },
            {
                "id": 2
            }
        ]
    },
    {   
        "id": 2,
        "title": "location 2",
        "items": [
            {
                "id": 3
            }
        ]
    }
]

Update: From your responses I derived:

const result = _(items)
    .groupBy(x => x.location.id)
    .map((value, key) => ({location: key, items: value}))
    .value();

Which works, but the item objects still contain the location attribute and the location only maintains its id attribute. Is it neccessary to add all attributes to the groupBy?

The reason I am doing this is because I would like to filter on item attributes, but display the items grouped by location.

4
  • Did you try anything? I see lodash tag. Commented Dec 8, 2017 at 14:12
  • Please show what you have tried. Stackoverflow isn't a free code writing service Commented Dec 8, 2017 at 14:12
  • 1
    Hi Vincent, guess I have asked a similar question before, check out: stackoverflow.com/questions/28220367/… The question was related to underscore.js, but perhaps it still helps. Commented Dec 8, 2017 at 14:13
  • It looks like you want us to write some code for you. While many users are willing to produce code for a coder in distress, they usually only help when the poster has already tried to solve the problem on their own. A good way to demonstrate this effort is to include the code you've written so far, example input (if there is any), the expected output, and the output you actually get (console output, tracebacks, etc.). The more detail you provide, the more answers you are likely to receive. Check the FAQ and How to Ask. Commented Dec 8, 2017 at 14:23

2 Answers 2

0

Collect the items using _.groupBy() on the location.title, then map them. To create an object, use the key (2nd param) for the location, take the id from the 1st item using _.get(), and map the other items to take just the location object without the title using _.omit():

const data = [{"id":1,"location":{"id":1,"title":"location 1"}},{"id":2,"location":{"id":1,"title":"location 1"}},{"id":3,"location":{"id":2,"title":"location 2"}}];

const result = _(data)
  .groupBy('location.title')
  .map((items, location) => ({
    location,
    id: _.get(items, '[0].id'), // get the 1st item (if any) id
    items: items.map(({ location }) => _.omit(location, 'title')) // map the objects and take the inner item without the title
  }))
  .value();
  
console.log(result);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.min.js"></script>

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

Comments

0

replace following code

const result = _(items)
    .groupBy(x => x.location.id)
    .map((value, key) => ({location: key, items: value}))
    .value();

with

const result = _(items)
    .groupBy(x => x.location.id)
    .map((value, key) => ({location: key, items: value.map(e => ({id: e.id}))}))
    .value();

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.