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:
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
.
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.
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.
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.
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.
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
}
}]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With