Home > Net >  want to display all maximum 'levelNumber' of objects in an array
want to display all maximum 'levelNumber' of objects in an array

Time:10-13

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

CodePudding user response:

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 => obj.levelNumber)))
const maxLevelObjects = arr1.filter(obj => obj.levelNumber === maxLevel)
console.log(maxLevelObjects);

CodePudding user response:

You can just iterate one time over arr1 with Array.prototype.reduce()

Code:

const arr1 = [{levelNumber: '2',name: 'abc',},{levelNumber: '3',name: 'abc',},{levelNumber: '3',name: 'raks'}]

const result = arr1.reduce((a, c) => {
  if (!a.length ||  c.levelNumber ===  a[0].levelNumber) {
    a.push(c)
  } else if ( c.levelNumber >  a[0].levelNumber) {
    a = [c]
  }

  return a
}, [])


console.log(result)

CodePudding user response:

    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);

CodePudding user response:

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

CodePudding user response:

const data = [
    {
        "levelNumber": "2",
        "name": "abc",
    },
    {
        "levelNumber": "3",
        "name": "abc"
    },
    { 
        "levelNumber": "3",
        "name": "raks",
    }
];

const levelNumbers = data.map((item) => parseInt(item.levelNumber));
const maxLevelNumber = Math.max(...levelNumbers).toString();

const highestLevelItems = data.filter((item) => item.levelNumber == maxLevelNumber);
console.log(highestLevelItems);

/* output
  [
    { levelNumber: '3', name: 'abc' },
    { levelNumber: '3', name: 'raks' }
  ]
*/

  • Related