简体   繁体   English

有没有更有效的方法来排序这个数组?

[英]Is there a more efficient way to sort this array?

So I have an array of a certain user's invites (invites he has received from other users).所以我有一系列特定用户的邀请(他从其他用户那里收到的邀请)。 And I want the frontend to sort these invites by their status.我希望前端按状态对这些邀请进行排序。 4 possible categories, expired, completed, accepted and pending. 4 个可能的类别,已过期、已完成、已接受和待处理。 The invites are fetched as objects from the DB.邀请作为对象从数据库中获取。 And this is how I sort them.这就是我对它们进行排序的方式。 The array partArray includes all of the invites and needs to be sorted into acceptedArr , expiredArr , completedArr and pendingArr .数组partArray包括所有邀请,需要分类为acceptedArrexpiredArrcompletedArrpendingArr

for(let j = partarray.length-1;j>=0;j--){



           if(partarray[j].userStats[auth.uid]==1 &&partarray[j].inviteStat==1){

             acceptedArr.push(partarray[j].id)
             partarray.splice(j, 1);

           }
           else if(partarray[j].userStats[auth.uid]==-1||partarray[j].inviteStat==0){

               expiredArr.push(partarray[j].id)
               partarray.splice(j, 1);


           }
           else if(partarray[j].userStats[auth.uid]==0&&partarray[j].inviteStat==1){

               pendingArr.push(partarray[j].id)
               partarray.splice(j, 1);


           }else if(partarray[j].inviteStat==2){


               completedArr.push(partarray[j].id)
               partarray.splice(j, 1);


           }


         }



        }

As you can see, in order for the invite to be sorted into the acceptedArr it needs to fulfil 2 conditions partarray[j].userStats[auth.uid]==1 && partarray[j].inviteStat==1 there are different conditions for each category.如您所见,为了将邀请分类到 acceptedArr 中,它需要满足 2 个条件partarray[j].userStats[auth.uid]==1 && partarray[j].inviteStat==1有不同的条件对于每个类别。 Is there a way to achieve what I'm doing in a more efficient manner?有没有办法以更有效的方式实现我正在做的事情?

Why not just filter them like this?为什么不像这样过滤它们呢?

 const acceptedArr = partarray.filter(invite => invite.userStats[auth.uid]===1 && invite.inviteStat===1).map(invite => invite.id); const completedArr = partarray.filter(invite => invite.inviteStat === 2).map(invite => invite.id);

I would avoid splitting array into 4 categories and use array.sort instead.我会避免将数组分成 4 个类别,而是使用array.sort

Here's the implementation with sample data.这是示例数据的实现。

 const auth = { uid: 'test' } const partarray = [{ userStats: { test: 1 }, inviteStat: 1 }, { userStats: { test: -1 }, inviteStat: 0 }, { userStats: { test: 0 }, inviteStat: 1 }, { userStats: { test: 1 }, inviteStat: 2 } ] const evaluateOrder = ({ userStats, inviteStat }) => { // lower value - higher ranking if (userStats[auth.uid] === 1 && inviteStat === 1) return 1; if (userStats[auth.uid] === -1 && inviteStat === 0) return 2; if (userStats[auth.uid] === 0 && inviteStat === 1) return 3; if (inviteStat === 2) return 4; } const sortUsers = (array) => { array.sort((prev, next) => { const prevOrder = evaluateOrder(prev); const nextOrder = evaluateOrder(next); return prevOrder - nextOrder; }) return array; } console.log(sortUsers(partarray))

Not sure if I understand what you're trying to do.不确定我是否理解你想要做什么。 You can eliminate a lot of that code with a switch statement though, and by moving the common code to the bottom:不过,您可以使用 switch 语句并通过将公共代码移至底部来消除大量此类代码:

for (let j = partarray.length - 1; j >= 0; j--) {

  let el = partarray[j]
  let userStats = el.userStats[auth.uid]
  let inviteStat = el.inviteStat

  switch(inviteStat){

      case 0:
          //do something
          break;

      case 1:
        if(userStats == 0){
           //do something
        } else if(userStats == 1){
           //do something
        }
        break;

      case 2:
          //do something
          break;
  }

    if (userStats == -1 ){
      //do something    
    } 

    pendingArr.push(el.id)
    partarray.splice(j, 1);
}

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

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