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

Javascript 如何使用ramda按数组对象中的最后一项排序,javascript,arraylist,ramda.js,Javascript,Arraylist,Ramda.js,这是一份父母名单,我想用ramda按2岁孩子的年龄对父母进行分类: [ { name: "Alicia", age: "43", children: [{ name: "Billy", age: "3" }, { name: "Mary", age: "8" }, ] }, { name: "Felicia", age: "60",

这是一份父母名单,我想用ramda按2岁孩子的年龄对父母进行分类:

[
  {
    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"))
            );
在vanilla JavaScript中(对格式相对较差的输入进行一些假设),使用以下方法:

但是要小心-如果父母的孩子少于2个,会发生什么情况?

在vanilla JavaScript中(对格式相对较差的输入进行一些假设),使用以下方法:


但是要小心-如果父对象的子对象少于2个,会发生什么情况?

假设上面的JSON是手工生成的,包括语法错误,然后假设您的真实数据刚刚好(一个父对象数组,每个父对象都有一个
子对象数组),那么正常的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);

假设上面的JSON是手工生成的,包括语法错误,然后假设实际数据很好(一个父数组,每个父数组都有一个
子对象
数组),那么正常的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);

使用
R.sortBy
并使用
R.pipe
创建的函数提取值。该函数使用
R.prop
获取对象的子数组,获取最后一个子数组(
R.last
),使用
R.propro
获取
年龄(如果没有子数组,则返回0),并转换为
数字。如果要颠倒顺序,可以使用
R.negate

const{sortBy,pipe,prop,last,propo}=R
常数fn=排序比(管道(
道具(“儿童”),
最后,,
比例(0,'年龄'),
数字,
//否定-如果你想颠倒顺序
))
康斯特父母=[{“姓名”:“艾丽西亚”,“年龄”:“43”,“儿童”:[{“姓名”:“比利”,“年龄”:“3”},{“姓名”:“玛丽”,“年龄”:“8”}]},{“姓名”:“菲利西亚”,“年龄”:“60”,“儿童”:[{“姓名”:“阿德里安”,“年龄”:“4”},{“姓名”:“约瑟夫”,“年龄”:“5”}]
const result=fn(父级)
console.log(结果)

使用
R.sortBy
并使用
R.pipe
创建的函数提取值。该函数使用
R.prop
获取对象的子数组,获取最后一个子数组(
R.last
),使用
R.propro
获取
年龄(如果没有子数组,则返回0),并转换为
数字。如果要颠倒顺序,可以使用
R.negate

const{sortBy,pipe,prop,last,propo}=R
常数fn=排序比(管道(
道具(“儿童”),
最后,,
比例(0,'年龄'),
数字,
//否定-如果你想颠倒顺序
))
康斯特父母=[{“姓名”:“艾丽西亚”,“年龄”:“43”,“儿童”:[{“姓名”:“比利”,“年龄”:“3”},{“姓名”:“玛丽”,“年龄”:“8”}]},{“姓名”:“菲利西亚”,“年龄”:“60”,“儿童”:[{“姓名”:“阿德里安”,“年龄”:“4”},{“姓名”:“约瑟夫”,“年龄”:“5”}]
const result=fn(父级)
console.log(结果)

我将使用
sortWith
ascend
函数。使用
sortWith
可以定义第一个排序顺序函数、第二个排序顺序函数等

const people=[
{
姓名:“艾丽西亚”,
年龄:"43岁",,
儿童:[{
姓名:“比利”,
年龄:“3”
},
{
姓名:“玛丽”,
年龄:“8”
},
]
},
{
姓名:“Felicia”,
年龄:"60岁",,
儿童:[{
姓名:“阿德里安”,
年龄:“4”
},
{
姓名:“约瑟夫”,
年龄:“5”
},
]
}
];
constby2ndchildage=ascend(pathOr(0,['儿童',1,'年龄]);
constby1stchildage=ascend(pathOr(0,['children',0,'age']);
log(sortWith([by2ndChildAge,by1stChildAge],people))


const{sortWith,ascend,pathOr}=R我将使用
sortWith
ascend
函数。使用
sortWith
可以定义第一个排序顺序函数、第二个排序顺序函数等

const people=[
{
姓名:“艾丽西亚”,
年龄:"43岁",,
儿童:[{
姓名:“比利”,
年龄:“3”
},
{
姓名:“玛丽”,
年龄:“8”
},
]
},
{
姓名:“Felicia”,
年龄:"60岁",,
儿童:[{
姓名:“阿德里安”,
年龄:“4”
},
{
姓名:“约瑟夫”,
年龄:“5”
},
]
}
];
constby2ndchildage=ascend(pathOr(0,['儿童',1,'年龄]);
constby1stchildage=ascend(pathOr(0,['children',0,'age']);
log(sortWith([by2ndChildAge,by1stChildAge],people))


const{sortWith,ascend,pathOr}=R我认为,最简单的解决方案就是结合:

const sortBy2ndChildAge=sortBy(路径(['children',1,'age']))
康斯特人=[{姓名:“艾丽西亚”,年龄:“43”,孩子:[{姓名:“比利”,年龄:“3”},{姓名:“玛丽”,年龄:“8”},{姓名:“费利西亚”,年龄:“60”,孩子:[姓名:“阿德里安”,年龄:“4”},{姓名:“约瑟夫”,年龄:“5”}]
console.log(sortBy2ndChildAge(people))


const{sortBy,path}=ramda
我认为,最简单的解决方案就是结合:

const sortBy2ndChildAge=sortBy(路径(['children',1,'age']))
康斯特人=[{姓名:“艾丽西亚”,年龄:“43”,孩子:[{姓名:“比利”,年龄:“3”},{姓名:“玛丽”,年龄:“8”},{姓名:“费利西亚”,年龄:“60”,孩子:[姓名:“阿德里安”,年龄:“4”},{姓名:“约瑟夫”,年龄:“5”}]
console.log(sortBy2ndChildAge(people))


const{sortBy,path}=ramda
您是只想使用ramda还是一个可以使用普通js代码的解决方案?我不介意看到其他解决方案!我更喜欢Ramda,因为我正在尝试学习它。它甚至不是有效的格式,mate,你的children属性如果它是一个对象,你可以像这样拥有它,这是普通JS已经可以使用
sort()
和一个比较函数作为参数来做的。然而,父母总是有两个孩子吗?(因为这似乎是一个不太可能的数据属性)您希望只使用ramda还是一个可以使用简单js代码的解决方案?我不介意看到其他解决方案!我只是更喜欢拉