0

I have an array made up of objects. Each object has two properties: name, value.

array = [
 {
  name: 'name1',
  value: 0
 },
 {
  name: 'name2',
  value: 2
 },
 {
  name: 'name3',
  value: 4
 },
 {
  name: 'name4',
  value: 4
 },
 {
  name: 'name5',
  value: 3
 },
 {
  name: 'name6',
  value: 2
 },
 {
  name: 'name7',
  value: 0
 },
 {
  name: 'name8',
  value: 1
 },
 ...
]

How do I get objects with the highest value property? In the above example I should restitute objects that have value = 4, value = 3, value = 2 (i.e. the first 3 largest values) i have tried something like this:

let first: 0
let second: 0
let third: 0
array.map((res: any) => {
    if (res.value > first) {
    third = second
    second = first
    first = res
} else if (res.value > second) {
    third = second
    second = res

} else if (res.value > third) {
    third = res
}
})

The problem is that if there are two or more objects with the same value, it does not return them both, but only one.

jacopotaba
  • 84
  • 10

3 Answers3

2

You need to sort the array first and then find all the elements containing top 3 values. For that you might need to keep an array checking if you have pushed all the elements containing the same value or not.

var pickedValues = [];
array = array
  .sort((a, b) => a.value > b.value ? -1 : 1)
  .filter(el => {
    if(pickedValues.length === 3 && !pickedValues.includes(el.value)) return false;

    if(!pickedValues.includes(el.value)) pickedValues.push(el.value);

    return true;
  });

Check the working code below:

var array = [{
    name: 'name1',
    value: 0
  },
  {
    name: 'name2',
    value: 2
  },
  {
    name: 'name3',
    value: 4
  },
  {
    name: 'name4',
    value: 4
  },
  {
    name: 'name5',
    value: 3
  },
  {
    name: 'name6',
    value: 2
  },
  {
    name: 'name7',
    value: 0
  },
  {
    name: 'name8',
    value: 1
  },
];

var pickedValues = [];
array = array
  .sort((a, b) => a.value > b.value ? -1 : 1)
  .filter(el => {
    if(pickedValues.length === 3 && !pickedValues.includes(el.value)) return false;
    
    if(!pickedValues.includes(el.value)) pickedValues.push(el.value);
    
    return true;
  });
  
  
console.log(array)
Archie
  • 901
  • 4
  • 11
0

Here is how you do it in detail.

let array = [
 {
  name: 'name1',
  value: 0
 },
 {
  name: 'name2',
  value: 2
 },
 {
  name: 'name3',
  value: 4
 },
 {
  name: 'name4',
  value: 4
 },
 {
  name: 'name5',
  value: 3
 },
 {
  name: 'name6',
  value: 2
 },
 {
  name: 'name7',
  value: 0
 },
 {
  name: 'name8',
  value: 1
 }];

let sorted = array.sort(function(a, b) {
    if(a.value > b.value) { return -1 };
    if(a.value < b.value) { return 1 };

    return 0;
});

console.table(array.splice(0, 3));
Vijay Joshi
  • 919
  • 7
  • 17
0

First you need to sort the array, you can use the default sort method, why we have taken the copy using spread syntax is basically we don't want to mutate the existing array.

Once you sort you can do a slice which will give you the required elements.

See the working code snippet

let array = [
 {
  name: 'name1',
  value: 0
 },
 {
  name: 'name2',
  value: 2
 },
 {
  name: 'name3',
  value: 4
 },
 {
  name: 'name4',
  value: 4
 },
 {
  name: 'name5',
  value: 3
 },
 {
  name: 'name6',
  value: 2
 },
 {
  name: 'name7',
  value: 0
 },
 {
  name: 'name8',
  value: 1
 }
]

let sortedArray = [...array].sort((a,b) => b.value - a.value)

// now the array is sorted on descending now you can choose the first three or how much i require


console.log(sortedArray.slice(0,3))
Learner
  • 8,379
  • 7
  • 44
  • 82