Group arrays by parent ids object Ramda

爱⌒轻易说出口 提交于 2020-01-06 02:20:15

问题


Need a help from Ramda community... I have an object array that I need to sort by "chapter_id" and after sorting, just remove "chapter_id":

const stuff = [
  { id: 1, title: "hello world", chapter_id: "4321" },
  { id: 2, title: "new title", chapter_id: "21" },
  { id: 3, title: "...", chapter_id: "33" },
  { id: 4, title: "huh!?", chapter_id: "14" },
  { id: 5, title: "From Earth", chapter_id: "11" },
  { id: 6, title: "alien", chapter_id: "11" },
  { id: 7, title: "Saturn", chapter_id: "11" },
  { id: 8, title: "Mars:/", chapter_id: "21" },
  { id: 9, title: "damn", chapter_id: "3" },
  { id: 10, title: "test", chapter_id: "11" },
  { id: 11, title: "ramda heeeelp", chapter_id: "31" },
  { id: 12, title: "hello?", chapter_id: "21" }
]

And as result I want to get this object:

{
  "3": [
    {
      "id": "9",
      "title": "damn"
    }
  ],
  "11": [
    {
      "id": "5",
      "title": "From Earth"
    },
    {
      "id": "6",
      "title": "alien"
    },
    {
      "id": "7",
      "title": "Saturn"
    },
    {
      "id": "10",
      "title": "test"
    }
  ],
  "14": [
    {
      "id": "4",
      "title": "huh!?"
    }
  ],
  "21": [
    {
      "id": "2",
      "title": "new title"
    },
    {
      "id": "8",
      "title": "Mars:/"
    },
    {
      "id": "12",
      "title": "hello?"
    }
  ],
  "31": [
    {
      "id": "11",
      "title": "ramda heeeelp"
    }
  ],
  "33": [
    {
      "id": "3",
      "title": "..."
    }
  ],
  "4321": [
    {
      "id": "1",
      "title": "hello world"
    }
  ]
}

How I struggled with this:

let object = {};

map(({ chapter_id }) => {
  const composed = compose(
    map(evolve({ id: toString })), //here id is converted to a string
    filter(c => c.chapter_id === chapter_id),
  );
  object[chapter_id] = composed(stuff)
}, stuff);

My result:

{
  "3": [
    {
      "id": "9",
      "title": "damn",
      "chapter_id": "3" //Dissoc this
    }
  ],
  "11": [
    {
      "id": "5",
      "title": "From Earth",
      "chapter_id": "11" //dissoc this
    },
    {
      "id": "6",
      "title": "alien",
      "chapter_id": "11"  //and this
    },
    {
      "id": "7",
      "title": "Saturn",
      "chapter_id": "11" //and this
    },
    {
      "id": "10",
      "title": "test",
      "chapter_id": "11" //and this
    }
  ],
  "14": [
    {
      "id": "4",
      "title": "huh!?",
      "chapter_id": "14" //and this
    }
  ],
  "21": [
    {
      "id": "2",
      "title": "new title",
      "chapter_id": "21" //and this
    },
    {
      "id": "8",
      "title": "Mars:/",
      "chapter_id": "21" //and this
    },
    {
      "id": "12",
      "title": "hello?",
      "chapter_id": "21" //and this
    }
  ],
  "31": [
    {
      "id": "11",
      "title": "ramda heeeelp",
      "chapter_id": "31" //and this!!!!!!
    }
  ],
  "33": [
    {
      "id": "3",
      "title": "...",
      "chapter_id": "33" //and this..
    }
  ],
  "4321": [
    {
      "id": "1",
      "title": "hello world",
      "chapter_id": "4321" //and this:(
    }
  ]
}

It works but I can't dissoc "chapter_id" from each object, does anyone know how to solve this? :Δ


回答1:


As Ori Drori notes, this is the likely Ramda method:

const transform = pipe (
  groupBy(prop('chapter_id')),
  map(map(dissoc('chapter_id'))),
)

But if you need to actually sort the keys, as your answer suggests, then it will take a bit more processing. You might try something like this:

const transform = pipe (
  groupBy(prop('chapter_id')),
  map(map(dissoc('chapter_id'))),
  toPairs,
  sortBy(pipe(head, Number)),
  fromPairs
)

const stuff = [{id: 1, title: "hello world", chapter_id: "4321"}, {id: 2, title: "new title", chapter_id: "21"}, {id: 3, title: "...", chapter_id: "33"}, {id: 4, title: "huh!?", chapter_id: "14"}, {id: 5, title: "From Earth", chapter_id: "11"}, {id: 6, title: "alien", chapter_id: "11"}, {id: 7, title: "Saturn", chapter_id: "11"}, {id: 8, title: "Mars: /", chapter_id: "21"}, {id: 9, title: "damn", chapter_id: "3"}, {id: 10, title: "test", chapter_id: "11"}, {id: 11, title: "ramda heeeelp", chapter_id: "31"}, {id: 12, title: "hello?", chapter_id: "21"}]

console.log (
  transform (stuff)
)
<script src="//cdnjs.cloudflare.com/ajax/libs/ramda/0.26.1/ramda.js"></script>
<script> const {pipe, groupBy, prop, map, dissoc, toPairs, sortBy, head, fromPairs} = R </script>

That sorting line could be written in many ways. Perhaps

sort(lift(subtract)(head, head)),

or just

sort(([a], [b]) => a - b),

And obviously if you were so inclined, you could pull out a sortByKeys function like this:

const sortByKeys = (fn) => pipe(toPairs, sortBy(fn), fromPairs)

sortByKeys is not a likely candidate for inclusion in Ramda, which really prefers to think of objects as unordered collections of name-value pairs. But it could easily go in your own helper library.




回答2:


With Ramda you can group by the key, and then map the groups, and dissoc the key from all objects:

const { pipe, groupBy, prop, map, dissoc } = R;

const fn = key => pipe(
  groupBy(prop(key)), // group by the key
  map(map(dissoc(key))) // remove the key from all objects in all groups
);

const stuff = [{"id":1,"title":"hello world","chapter_id":"4321"},{"id":2,"title":"new title","chapter_id":"21"},{"id":3,"title":"...","chapter_id":"33"},{"id":4,"title":"huh!?","chapter_id":"14"},{"id":5,"title":"From Earth","chapter_id":"11"},{"id":6,"title":"alien","chapter_id":"11"},{"id":7,"title":"Saturn","chapter_id":"11"},{"id":8,"title":"Mars:/","chapter_id":"21"},{"id":9,"title":"damn","chapter_id":"3"},{"id":10,"title":"test","chapter_id":"11"},{"id":11,"title":"ramda heeeelp","chapter_id":"31"},{"id":12,"title":"hello?","chapter_id":"21"}];

const result = fn('chapter_id')(stuff);

console.log(result);
<script src="https://cdnjs.cloudflare.com/ajax/libs/ramda/0.26.1/ramda.js"></script>



回答3:


First of all the finite task is not properly described :) What you really want (based on the result I want to get section) is to normalize (or restructure) the array of objects into an object, which have the chapter_id as a key, and the value is an array of the associated records of the stuff array with that chapter_id

On my opinion, your solution is cool and seems like a more functional, but in this particular case I've probably will give a preference to the simple reduce function, which is more readable...

reduce((acc, {chapter_id, ...rest}) => {
  const isInitialized = !!acc[chapter_id];
  if (isInitialized) {
    acc[chapter_id].push(rest); 
  } else {
    acc[chapter_id] = [rest];
  }
  return acc;
}, {}, stuff);


来源:https://stackoverflow.com/questions/58682137/group-arrays-by-parent-ids-object-ramda

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!