https://github.com/jsa2/arraygroupitems
https://github.com/jsa2/arraygroupitems
Last synced: about 2 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/jsa2/arraygroupitems
- Owner: jsa2
- Created: 2021-12-21T06:17:18.000Z (over 4 years ago)
- Default Branch: public
- Last Pushed: 2021-12-21T06:43:04.000Z (over 4 years ago)
- Last Synced: 2026-01-02T00:54:56.388Z (5 months ago)
- Language: JavaScript
- Size: 10.7 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
Awesome Lists containing this project
README
# Data grouping tool
There is likely multiple libraries to do this same thing, I did this mostly for fun, and since I need to customize sorting to certain output type very often.
✅ Data output and input is expected to be JSON
✅ Tool will add group and items object to each group

```JS
{
group: "Kumpula",
items: [
[
{
group: "sports",
items: [
{
"......":"....."
},
],
},
],
],
}
```
## Example using https://open-api.myhelsinki.fi/
## How to use
1. Make the sortable data available in single level (Node.JS array.map() is handy for this
2. Decide grouping criteria
```js
const { newSet } = require("./helpers/grouping");
const { httpP } = require("./helpers/httpabstract");
main()
async function main () {
var data = await httpP('http://open-api.myhelsinki.fi/v2/places/').catch((error) => {
console.log(error)
})
var places = JSON.parse(data)
// make sortable data "flat"
places.data.map(place => {
//console.log(place)
place.postal = place.location.address.postal_code
place.neighbourhood = place.location.address.neighbourhood
place.tag = place.tags[0].name
})
// Additional sort for post number
var sortedSet = newSet(places.data, ['postal','neighbourhood','tag']).sort((a,b) => {
if (a.group < b.group) {
return -1
} else {
return 1
}
})
console.log(sortedSet)
}
```