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. 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
.
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. 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.
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:
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);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.