Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Filter and sort a JavaScript array

I have an array like this:

[{
    "number": "4",
    "fileName": "fileXX",
    "rating": {
        "average": 6.4
    }
}, {
    "number": "3",
    "fileName": "fileXX",
    "rating": {
        "average": 5.4
    }
}, {
    "number": "4",
    "fileName": "fileXX",
    "rating": {
        "average": 5.4
    }
}]

I am trying to create a new array with the following criteria:

  1. Get highest rating (array.rating.average) of each number (array.number)

Output should be:

[{
    "number": "4",
    "fileName": "fileXX",
    "rating": {
        "average": 6.4
    }
}, {
    "number": "3",
    "fileName": "fileXX",
    "rating": {
        "average": 5.4
    }
}
}]

I have just managed to sort by highest rating:

array.sort(function(a , b) {
    return a.rating.average - b.rating.average;
});
array.reverse();

But, now, I just only want one object per duplicate array.number, keeping the one that has the highest array.rating.average.

like image 467
James Avatar asked Sep 03 '16 03:09

James


People also ask

How do you filter an array in JavaScript?

One can use filter() function in JavaScript to filter the object array based on attributes. The filter() function will return a new array containing all the array elements that pass the given condition. If no elements pass the condition it returns an empty array.

Can you sort an array in JavaScript?

The sort() method sorts the elements of an array in place and returns the reference to the same array, now sorted. The default sort order is ascending, built upon converting the elements into strings, then comparing their sequences of UTF-16 code units values.

How do you sort an array array?

To sort the array of arrays, you need to specify based on which element you want to sort them. Here we compare the arrays by their second elements. Then the sort() function loops through the array of arrays and sorts it based on the magnitude of the second elements.

How do you sort an array of objects in JavaScript?

To sort an array of objects in JavaScript, use the sort() method with a compare function. A compare function helps us to write our logic in the sorting of the array of objects. They allow us to sort arrays of objects by strings, integers, dates, or any other custom property.


1 Answers

array.sort((a, b) => {
  if(a.number === b.number) {
    // If two elements have same number, then the one who has larger rating.average wins
    return b.rating.average - a.rating.average;
  } else {
    // If two elements have different number, then the one who has larger number wins
    return b.number - a.number;
  }
});


array = array.filter((element, index) => {
  return index === 0 || element.number !== array[index-1].number;
});

For your test case,

[{
  "number": "4",
  "fileName": "fileXX",
  "rating": {
    "average": 6.4
  }
}, {
  "number": "3",
  "fileName": "fileXX",
  "rating": {
    "average": 5.4
  }
}, {
  "number": "4",
  "fileName": "fileXX",
  "rating": {
    "average": 5.4
  }
}]

After sorting, the output would be

[{
  "number": "4",
  "fileName": "fileXX",
  "rating": {
    "average": 6.4
  }
}, {
  "number": "4",
  "fileName": "fileXX",
  "rating": {
    "average": 5.4
  }
}, {
  "number": "3",
  "fileName": "fileXX",
  "rating": {
    "average": 5.4
  }
}]

And after filter, the final result:

[{
  "number": "4",
  "fileName": "fileXX",
  "rating": {
    "average": 6.4
  }
}, {
  "number": "3",
  "fileName": "fileXX",
  "rating": {
    "average": 5.4
  }
}]
like image 84
Haibara Ai Avatar answered Oct 21 '22 01:10

Haibara Ai