1

I have an array like this

const allChampions = 
  [ { champion: 'Vayne', percentage:  33.33, role: 'top'     } 
  , { champion: 'Jinx',  percentage:   0,    role: 'jungle'  } 
  , { champion: 'Vayne', percentage:   0,    role: 'mid'     } 
  , { champion: 'Jinx',  percentage: 100,    role: 'bottom'  } 
  , { champion: 'Vayne', percentage:   0,    role: 'support' } 
  ] 

I want to filter out the array by checking for duplicate champions. If the champion is a duplicate, I want to remove the champion with the lowest percentage.

I've tried filtering the array into a new one using this:

let sortedChampions = [];
sortedChampionsWithDuplicates.map((x) =>
    sortedChampions.filter((a) => a.champion == x.champion && a.percentage >= x.percentage).length > 0 ? null : sortedChampions.push(x)
);

But this is what the end result ends up being:

[{"champion":"Vayne","percentage":33.33,"role":"top"},{"champion":"Jinx","percentage":0,"role":"jungle"},{"champion":"Jinx","percentage":100,"role":"bottom"}]
3
  • 4
    Please show us what you have tried. SO isn't a free code writing service. The objective here is for you to post your attempts to solve your own issue and others help when they don't work as expected. See How to Ask and creating a minimal reproducible example Commented Jun 16, 2021 at 0:16
  • this is unclear, you want to create a new array with the correct values or clean up the existing array ? Commented Jun 16, 2021 at 0:24
  • I just posted what I have attempted so far. I know that I can probably solve this with some for loops and conditional statements, but I want to see if there's a cleaner way to do it. Commented Jun 16, 2021 at 0:38

2 Answers 2

2

I'm assuming this is just a quick script for your own purposes, so something like this might work:

const removeDupes = allChampions.reduce((acc, champStats) => {
  if (acc[champStats.champion]) {
    acc[champStats.champion] = acc[champStats.champion].percentage > champStats.percentage ? acc[champStats.champion] : champStats
    return acc
  } else {
    acc[champStats.champion] = champStats
    return acc
  }
}, {})

console.log(Object.values(removeDupes))
Sign up to request clarification or add additional context in comments.

Comments

1

you can do that...

const allChampions = 
  [ { champion: 'Vayne', percentage:  33.33, role: 'top'     } 
  , { champion: 'Jinx',  percentage:   0,    role: 'jungle'  } 
  , { champion: 'Vayne', percentage:   0,    role: 'mid'     } 
  , { champion: 'Jinx',  percentage: 100,    role: 'bottom'  } 
  , { champion: 'Vayne', percentage:   0,    role: 'support' } 
  ] 

for (let i=allChampions.length;i--;) 
  {
  let curr   = allChampions[i]
    , bigger = allChampions.find(x=>x.champion===curr.champion && x.percentage > curr.percentage)
  if (bigger) 
    allChampions.splice(i,1)
  }

console.log( allChampions )

OR, if you want to create a new array:

const allChampions = 
  [ { champion: 'Vayne', percentage:  33.33, role: 'top'     } 
  , { champion: 'Jinx',  percentage:   0,    role: 'jungle'  } 
  , { champion: 'Vayne', percentage:   0,    role: 'mid'     } 
  , { champion: 'Jinx',  percentage: 100,    role: 'bottom'  } 
  , { champion: 'Vayne', percentage:   0,    role: 'support' } 
  ] 

const sortedChampions =
  allChampions.filter( (c,_,t) =>
    !t.find( x => x.champion === c.champion 
               && x.percentage > c.percentage )
                     )
  
console.log( sortedChampions )

OR, if you doesn't want to use a find :

const allChampions = 
  [ { champion: 'Vayne', percentage:  33.33, role: 'top'     } 
  , { champion: 'Jinx',  percentage:   0,    role: 'jungle'  } 
  , { champion: 'Vayne', percentage:   0,    role: 'mid'     } 
  , { champion: 'Jinx',  percentage: 100,    role: 'bottom'  } 
  , { champion: 'Vayne', percentage:   0,    role: 'support' } 
  ] 

const result = Object.values(allChampions.reduce((acc, curr) =>
  {
  if ((acc[curr.champion]?.percentage || -1) < curr.percentage ) 
    acc[curr.champion] = curr
  return acc
  }, {})) 

console.log( result )

2 Comments

Works great. I didn't know you could write a for loop with just two statements, I thought you needed three. Pretty cool. Thank you.
@JuniorYono there is 3 statements in the loop, the third one is an empty statemet

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.