Javascript 按一个对象属性的长度对对象数组进行排序

Javascript 按一个对象属性的长度对对象数组进行排序,javascript,arrays,Javascript,Arrays,我有一个对象数组。在每个卡片中,对象都有一个属性名votedBy,该属性存储投票人的用户名。我想按属性votedBy的长度对数组进行排序。注意:某些对象没有votedBy属性 [{ id: 'card1', text: "Item1", votedBy: { userId1: true, userId2: true } }, { id: 'card2', text: 'Item 1', vot

我有一个对象数组。在每个卡片中,对象都有一个属性名votedBy,该属性存储投票人的用户名。我想按属性votedBy的长度对数组进行排序。注意:某些对象没有votedBy属性

[{
  id: 'card1',
  text: "Item1",
  votedBy: {
             userId1: true,
             userId2: true
           }
 }, 
 {
  id: 'card2',
  text: 'Item 1',
  votedBy: {
             userId3: true
           }
 }, 
 {
  id: 'card3',
  text: 'Item 3'
 }, 
{
  id: 'card4',
  text: "Item4",
  votedBy: {
             userId5: true,
             userId6: true,
             userId7: true
           }
 }]
我尝试使用Array.sort,如下所示

array.sort((a,b) => Object.keys(b.votedBy).length - Object.keys(a.votedBy).length )
[{
     {
      id: 'card4',
      text: "Item4",
      votedBy: {
                 userId5: true,
                 userId6: true,
                 userId7: true
               }
     },
    {
      id: 'card1',
      text: "Item1",
      votedBy: {
                 userId1: true,
                 userId2: true
               }
     }, 
     {
      id: 'card2',
      text: 'Item 1',
      votedBy: {
                 userId3: true
               }
     }, 
     {
      id: 'card3',
      text: 'Item 3'
     }, 
]
而且,只有当每个对象都必须具有votedBy属性时,它才起作用。但是我的一些对象没有这个属性

[{
  id: 'card1',
  text: "Item1",
  votedBy: {
             userId1: true,
             userId2: true
           }
 }, 
 {
  id: 'card2',
  text: 'Item 1',
  votedBy: {
             userId3: true
           }
 }, 
 {
  id: 'card3',
  text: 'Item 3'
 }, 
{
  id: 'card4',
  text: "Item4",
  votedBy: {
             userId5: true,
             userId6: true,
             userId7: true
           }
 }]
结果应该是这样的

array.sort((a,b) => Object.keys(b.votedBy).length - Object.keys(a.votedBy).length )
[{
     {
      id: 'card4',
      text: "Item4",
      votedBy: {
                 userId5: true,
                 userId6: true,
                 userId7: true
               }
     },
    {
      id: 'card1',
      text: "Item1",
      votedBy: {
                 userId1: true,
                 userId2: true
               }
     }, 
     {
      id: 'card2',
      text: 'Item 1',
      votedBy: {
                 userId3: true
               }
     }, 
     {
      id: 'card3',
      text: 'Item 3'
     }, 
]
更新

array.sort((a, b) => (
  Boolean(b.votedBy) - Boolean(a.votedBy)
  || Object.keys(b.votedBy).length - Object.keys(a.votedBy).length
));
只有当我有一个没有sortedBy的对象时,它才起作用。如果我有多个没有sortedBy的对象,则有一个错误

未捕获的TypeError:无法将未定义或null转换为对象

新的测试阵列应该是这样的

[{
  id: 'card1',
  text: "Item1",
  votedBy: {
             userId1: true,
             userId2: true
           }
 }, 
 {
  id: 'card2',
  text: 'Item 1',
  votedBy: {
             userId3: true
           }
 }, 
 {
  id: 'card3',
  text: 'Item 3'
 }, 
{
  id: 'card4',
  text: "Item4",
  votedBy: {
             userId5: true,
             userId6: true,
             userId7: true
           }
 },
 {
  id: 'card5',
  text: 'Item 5'
 } ]
更新2

我通过长而难看的代码使它工作。有没有人有更好更短的代码

array.sort((a, b) => {
  if (a.votedBy === undefined && b.votedBy === undefined)
  return Boolean(b.votedBy) - Boolean(a.votedBy)
  else if (a.votedBy === undefined) 
  return Object.keys(b.votedBy).length - Boolean(a.votedBy)
  else if (b.votedBy === undefined) 
  return Boolean(b.votedBy) - Object.keys(a.votedBy).length
  else return Object.keys(b.votedBy).length - Object.keys(a.votedBy).length
});

首先,根据是否存在
b.votedBy
a.votedBy
之间的差异进行排序:

array.sort((a, b) => (
  Boolean(b.votedBy) - Boolean(a.votedBy)
  || (
    a.votedBy &&
    Object.keys(b.votedBy).length - Object.keys(a.votedBy).length
  )
));
const数组=[{
id:'card1',
正文:“项目1”,
投票人:{
userId1:是的,
userId2:true
}
},
{
id:'card2',
正文:“项目1”,
投票人:{
userId3:true
}
},
{
id:'卡3',
正文:“项目3”
},
{
id:'card4',
案文:“项目4”,
投票人:{
userId5:是的,
userId6:是的,
userId7:对
}
},
{id:'card5',text:'Item 5'}
];
array.sort((a,b)=>(
布尔(b.votedBy)-布尔(a.votedBy)
|| (
a、 沃特比&&
Object.keys(b.votedBy).length-Object.keys(a.votedBy).length
)
));

console.log(数组)
第一种排序方式是根据是否存在
b.votedBy
a.votedBy
之间的差异:

array.sort((a, b) => (
  Boolean(b.votedBy) - Boolean(a.votedBy)
  || (
    a.votedBy &&
    Object.keys(b.votedBy).length - Object.keys(a.votedBy).length
  )
));
const数组=[{
id:'card1',
正文:“项目1”,
投票人:{
userId1:是的,
userId2:true
}
},
{
id:'card2',
正文:“项目1”,
投票人:{
userId3:true
}
},
{
id:'卡3',
正文:“项目3”
},
{
id:'card4',
案文:“项目4”,
投票人:{
userId5:是的,
userId6:是的,
userId7:对
}
},
{id:'card5',text:'Item 5'}
];
array.sort((a,b)=>(
布尔(b.votedBy)-布尔(a.votedBy)
|| (
a、 沃特比&&
Object.keys(b.votedBy).length-Object.keys(a.votedBy).length
)
));

console.log(数组)小问题,如果我向数组添加一个新对象{id:'card5',text:'item5'}。它显示错误:未捕获类型错误:无法将未定义或null转换为对象。因此,只有当数组中只有1个对象没有votedBy时,您的解决方案才有效?导致此错误的原因是您添加的对象没有
votedBy
属性-如果添加这样的对象,算法应该如何运行?没有sortedBy的对象应该位于数组的底部result@tomen哦,没错,如果有多个对象没有
votedBy
属性,则它们将相互排序,因此布尔减法的计算结果将为0。如果您不关心没有
votedBy
的对象是如何相互排序的,您可以在比较键的长度之前,添加另一个检查,确保
a.votedBy
是真实的,请参见editTruthly Impression,我已经尝试了几个小时来编写这样的简短而干净的代码,但仍然没有找到答案。如果我在数组中添加一个新的对象{id:'card5',text:'Item 5'},我会有一个小问题。它显示错误:未捕获类型错误:无法将未定义或null转换为对象。因此,只有当数组中只有1个对象没有votedBy时,您的解决方案才有效?导致此错误的原因是您添加的对象没有
votedBy
属性-如果添加这样的对象,算法应该如何运行?没有sortedBy的对象应该位于数组的底部result@tomen哦,没错,如果有多个对象没有
votedBy
属性,则它们将相互排序,因此布尔减法的计算结果将为0。如果您不关心没有
votedBy
的对象是如何相互排序的,您可以在比较键的长度之前,添加另一个检查,确保
a.votedBy
是真实的,请参见editTruthly Impression,我已经尝试了几个小时来编写这样的简短而干净的代码,但仍然没有找到答案。非常感谢