通过父ID对象Ramda对数组进行分组

时间:2019-11-03 16:21:16

标签: javascript functional-programming ramda.js

需要Ramda社区的帮助...我有一个对象数组,需要按“ chapter_id”进行排序,排序后,只需删除“ 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”,有人知道如何解决吗? :Δ

3 个答案:

答案 0 :(得分:3)

使用Ramda,您可以按const handleChange = prop => event => { setValues({ ...values, [prop]: event.target.value }) if (prop === 'newpassword' && passwordValidation.test(values.newpassword)) { setValues({ ...values, passwordisValid: true }) console.log(prop, values.passwordisValid) } else { console.log(prop, values.passwordisValid) } } 分组,然后映射组,并从所有对象中分离key

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

答案 1 :(得分:2)

首先,没有正确描述有限任务:)您真正想要的(基于我要获得的结果部分)是归一化(或重组)数组。对象,将对象chapter_id作为键,其值是stuff数组中与该记录chapter_id相关的记录的数组

我认为您的解决方案很酷,而且似乎更实用,但是在这种特殊情况下,我可能会偏爱简单的reduce函数,该函数更具可读性...

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

答案 2 :(得分:2)

如Ori Drori所述,这可能是Ramda方法:

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

但是,如果您需要如答案所示对键进行实际排序,则将需要更多的处理。您可以尝试这样的事情:

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>

排序行可以用多种方式编写。也许

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

或者只是

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

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

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

sortByKeys不太可能包含在Ramda中,Ramda确实更喜欢将对象视为名称/值对的无序集合。但是它可以轻松进入您自己的帮助器库。