Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

114
Views
want to display all maximum 'levelNumber' of objects in an array
arr1 = [
{
    "levelNumber": "2",
    "name": "abc",
},
{
    "levelNumber": "3",
    "name": "abc"
},
{ 
    "levelNumber": "3",
    "name": "raks",
}
]

my result array should have objects with max levelNumber i.e 3 in this case.

it should look like:

resultArr = [
{
    "levelNumber": "3",
    "name": "abc"
},
{ 
    "levelNumber": "3",
    "name": "raks",
}
]

note that here levelNumber can be anything.. please help me with the generic nodejs code to get duplicate max value objects

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

You can first find the max level of all the objects in the array and then filter the array

arr1 = [
{
    "levelNumber": "2",
    "name": "abc",
},
{
    "levelNumber": "3",
    "name": "abc"
},
{ 
    "levelNumber": "3",
    "name": "raks",
}
]
const maxLevel = String(Math.max(...arr1.map(obj => Number(obj.levelNumber))))
const maxLevelObjects = arr1.filter(obj => obj.levelNumber === maxLevel)
console.log(maxLevelObjects);

about 4 years ago · Juan Pablo Isaza Report

0

    const arr1 = [
      {
        levelNumber: '2',
        name: 'abc',
      },
      {
        levelNumber: '3',
        name: 'abc',
      },
      {
        levelNumber: '3',
        name: 'raks',
      },
    ];
    
    const getHighLevelElements = (array) => {
      if (array.length === 0) return null;
      array.sort((elem1, elem2) => {
        if (Number(elem1.levelNumber) < Number(elem2.levelNumber)) {
          return 1;
        }
        if (Number(elem1.levelNumber) > Number(elem2.levelNumber)) {
          return -1;
        }
        return 0;
      });
    
      return array.filter((elem) => elem.levelNumber === array[0].levelNumber);
    };
    
    const resultArr = getHighLevelElements([...arr1]);
    console.log(resultArr);

about 4 years ago · Juan Pablo Isaza Report

0

I would first have a variable called highestLevel to store the highest level number found in the array of objects (will be used later while looping), loop through the whole array and checking every key levelNumber and storing that number IF highestLevel is lower than the value of the current object levelNumber. After I've looped through the array and got the actual highestLevel number, I would loop through again and only get the objects that are equivalent to my variable highestLevel

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!