-2

I have array of objects that I need to sort it in JavaScript the way that suits me.

this is example of array that I have:

[
  {
    "id": "AD012014001R",
    "country": "AD",
    "series": 1,
    "issue_year": 2014
  },
  {
    "id": "AD012014002R",
    "country": "AD",
    "series": 1,
    "issue_year": 2014
  },
  {
    "id": "AT012002002R",
    "country": "AT",
    "series": 1,
    "issue_year": 2002
  },
  {
    "id": "BE011999100R",
    "country": "BE",
    "series": 1,
    "issue_year": 1999
  },
  {
    "id": "BE011999200R",
    "country": "BE",
    "series": 1,
    "issue_year": 1999
  },
  {
    "id": "BE022008001R",
    "country": "BE",
    "series": 2,
    "issue_year": 2008
  }
]

and my output should look like this:

[
  {
    "country": "AD",
    "series": [
      {
        "series": 1,
        "coins": [
          {
            "id": "AD012014001R",
            "country": "AD",
            "series": 1,
            "issue_year": 2014
          },
          {
            "id": "AD012014002R",
            "country": "AD",
            "series": 1,
            "issue_year": 2014
          }
        ]
      }
    ]
  },
  {
    "country": "AT",
    "series": [
      {
        "series": 1,
        "coins": [
          {
            "id": "AT012002002R",
            "country": "AT",
            "series": 1,
            "issue_year": 2002
          }
        ]
      }
    ]
  },
  {
    "country": "BE",
    "series": [
      {
        "series": 1,
        "coins": [
          {
            "id": "BE011999100R",
            "country": "BE",
            "series": 1,
            "issue_year": 1999
          },
          {
            "id": "BE011999200R",
            "country": "BE",
            "series": 1,
            "issue_year": 1999
          }
        ]
      },
      {
        "series": 2,
        "coins": [
          {
            "id": "BE022008001R",
            "country": "BE",
            "series": 2,
            "issue_year": 2008
          }
        ]
      }
    ]
  }
]

So my API response is must bigger but this is small part of it. I am getting array of hundred of objects that look like this and need to sort them and return them sorted first by countries and then by series under each country. I know there is lot of similar questions but I can't figure it out because my problem seams little more complicated than all of them.

pilchard
  • 12,414
  • 5
  • 11
  • 23
Klak031
  • 97
  • 1
  • 1
  • 13
  • Search for grouping, not sorting. Also, there are no multidimensional arrays here, nor will you be working with JSON in any meaningful way, unless you want to try to do this using string manipulation... – Heretic Monkey Mar 28 '23 at 20:51
  • 1
    and [Most efficient method to groupby on an array of objects](https://stackoverflow.com/questions/14446511/most-efficient-method-to-groupby-on-an-array-of-objects) – pilchard Mar 28 '23 at 20:52
  • 1
    or multi-level: [JavaScript - Group Array by value and upto two levels](https://stackoverflow.com/questions/62416165/javascript-group-array-by-value-and-upto-two-levels) or [Group array of objects by two properties](https://stackoverflow.com/questions/75356124/group-array-of-objects-by-two-properties) – pilchard Mar 28 '23 at 20:53

1 Answers1

1

You could take a multi ruping approach by having an array of group key and children property. Then find the group at level and build a new object or just return the children. At the end push the object.

const
    data = [{ id: "AD012014001R", country: "AD", series: 1, issue_year: 2014 }, { id: "AD012014002R", country: "AD", series: 1, issue_year: 2014 }, { id: "AT012002002R", country: "AT", series: 1, issue_year: 2002 }, { id: "BE011999100R", country: "BE", series: 1, issue_year: 1999 }, { id: "BE011999200R", country: "BE", series: 1, issue_year: 1999 }, { id: "BE022008001R", country: "BE", series: 2, issue_year: 2008 }],
    groups = [
        ['country', 'series'],
        ['series', 'coins']
    ],
    result = data.reduce((r, o) => {
        groups
            .reduce((level, [key, children]) => {
                let group = level.find(q => q[key] === o[key]);
                if (!group) level.push(group = { [key]: o[key], [children]: [] });
                return group[children];
            }, r)
            .push(o);
        return r;
    }, []);

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392