本文介绍了通过父ID对象Ramda对数组进行分组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

需要Ramda社区的帮助...我有一个对象数组,需要按"chapter_id"进行排序,排序后,只需删除"chapter_id"即可:

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" }
]


因此,我想获取该对象:

{
  "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"
    }
  ]
}

我如何为此苦苦挣扎:

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);

我的结果:

{
  "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:(
    }
  ]
}

它可以工作,但是我不能从每个对象中释放"chapter_id",有人知道如何解决吗? :Δ

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

推荐答案

正如Ori Drori指出的那样,这可能是Ramda方法:

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)),

或者只是

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

很显然,如果您如此愿意,可以拉出一个sortByKeys函数,如下所示:

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不太可能包含在Ramda中,Ramda确实更喜欢将对象视为名称/值对的无序集合.但这可以轻松地放入您自己的帮助器库中.

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.

这篇关于通过父ID对象Ramda对数组进行分组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-09 14:47