繁体   English   中英

如何使用ramda按数组对象中的最后一项排序

[英]How to use ramda to sort by last item in an array object

以下是父母名单,我想按拉姆达分类按其第二个孩子的年龄进行排序:

[
  {
    name: "Alicia",
    age: "43",
    children: [{
        name: "Billy",
        age: "3"
      },
      {
        name: "Mary",
        age: "8"
      },
    ]
  },
  {
    name: "Felicia",
    age: "60",
    children: [{
        name: "Adrian",
        age: "4"
      },
      {
        name: "Joseph",
        age: "5"
      },
    ]
  }
]

我该如何处理? 我尝试按照以下方式做一些事情

parents.sort(
                sortBy("-children.age"))
            );

使用R.sortBy并提取与函数与创造的价值R.pipe 该函数使用R.prop获取对象的children数组,获取最后一个孩子( R.last ),使用R.propOr获取age (如果没有孩子则返回0),然后转换为Number 如果要R.negate顺序,可以使用R.negate

 const { sortBy, pipe, prop, last, propOr } = R const fn = sortBy(pipe( prop('children'), last, propOr(0, 'age'), Number, // negate - if you want to reverse the order )) const parents = [{"name":"Alicia","age":"43","children":[{"name":"Billy","age":"3"},{"name":"Mary","age":"8"}]},{"name":"Felicia","age":"60","children":[{"name":"Adrian","age":"4"},{"name":"Joseph","age":"5"}]}] const result = fn(parents) console.log(result) 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/ramda/0.26.1/ramda.js"></script> 

在原始JavaScript中(使用一些相对格式较差的假设),使用Array.prototype.sort方法:

let parents = [ .... ]; // What you have above
parents = parents.sort((a, b) => {
  return a.children[1].age - b.children[1].age; // Change - to + for ascending / descending
});

不过要小心-如果父母少于两个孩子会怎样?

假设上面的JSON是手工生成的,包括语法错误,然后假设您的真实数据很好(一个父级数组,每个父级都有一个对象的children级数组),那么普通的JS排序就可以了:

const compareC2(parent1, parent2) {
  let c1 = parent1.children;
  let c2 = parent2.children;

  if (!c1 || !c2) {
    // what happens if someone has no children?
  }

  let l1 = c1.length;
  let l2 = c2.length;

  if (l1 === 0 || l2 === 0) {
    // different symptom, but same question as above
  }

  if (l1 !== l2) {
    // what happens when the child counts differ?
  }

  if (l1 !== 2) {
    // what happens when there are fewer, or more than, 2 children?
  }

  // after a WHOLE LOT of assumptions, sort based on
  // the ages of the 2nd child for each parent.
  return c1[1].age - c2[1].age;
}  

let sorted = parents.sort(compareC2);

我会用sortWithascend的功能。 使用sortWith允许您定义第一个排序顺序函数,第二个排序顺序函数等。

 const people = [ { name: "Alicia", age: "43", children: [{ name: "Billy", age: "3" }, { name: "Mary", age: "8" }, ] }, { name: "Felicia", age: "60", children: [{ name: "Adrian", age: "4" }, { name: "Joseph", age: "5" }, ] } ]; const by2ndChildAge = ascend(pathOr(0, ['children', 1, 'age'])); const by1stChildAge = ascend(pathOr(0, ['children', 0, 'age'])); console.log(sortWith([by2ndChildAge, by1stChildAge], people)); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/ramda/0.26.1/ramda.min.js"></script> <script>const {sortWith, ascend, pathOr} = R;</script> 

我认为,最简单的解决方案是将sortBypath结合使用:

 const sortBy2ndChildAge = sortBy(path(['children', 1, 'age'])) const people = [{name: "Alicia", age: "43", children: [{name: "Billy", age: "3"}, {name: "Mary", age: "8"}]}, {name: "Felicia", age: "60", children: [{name: "Adrian", age: "4"}, {name: "Joseph", age: "5"}]}] console.log(sortBy2ndChildAge(people)) 
 <script src="https://bundle.run/ramda@0.26.1"></script><script> const {sortBy, path} = ramda </script> 

其他人已经指出,这样做存在一些潜在的缺陷。 父母是否总是保证至少有两个孩子? 我们是否真的要按字典顺序排序-即'11' < '2' 2'-还是要将这些值转换为数字?

解决这两个问题很容易: sortBy(compose(Number, pathOr(0, ['children', 1, 'age']))) ,但这取决于您要执行的操作。 如果您只是用来学习sortBy ,那么sortBypath都是有用的函数。 当您可以将要排序的项目转换为某种有序类型时, sortBy很有用-字符串,数字,日期或带有数值valueOf方法的任何内容。 您提供该转换函数和值列表,它将按此排序。 path只是对对象中的嵌套属性列表的null安全读取。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM