简体   繁体   中英

Ranking objects in array javascript

I have an API that upon request fetches all objects (movies) from the database. Each object has several ratings, which are used to compute one main rating for the object. Each object is then pushed into an array. Before the JSON-response is sent, this array is sorted based on the main rating. The highest rating is ranked first.

What I want is that each object also has the rank its at. So the object with the highest rating should also have a property along the lines of: "ranking": 1.

I am just confused by how I can achieve this before sending the response. This is my code:

const moviesListByRank = function (req, res) { 
let movieslist = [];
let movieMainRating = null;
Movie.find({}, function(err, movies){
    if(err) {
        console.log(err)
    } else {
        movies.forEach(function(movie){
            movieslist.push({
                name: movies.name,
                category: movie.category,
                movieMainRating: ((movie.FilterAction + hobby.FilterAdventure + movie.FilterRomantic)/3).toFixed(2),
                FilterAction: movie.FilterAction,
                FilterAdventure: movie.FilterAdventure,
                FilterRomatic: movie.FilterRomantic,
                _id: hobby._id
            });
        });
    }
    function compare(a,b){
        if (b.movieMainRating < a.movieMainRating)
            return -1;
        if (b.movieMainRating > a.movieMainRating)
            return 1;
        return 0;
    }
    movieslist.sort(compare);

    res
        .status(200)
        .json(movieslist);
});
};

A simple approach is to; after movieslist.sort(compare); create another for loop, the forEach also returns an index which you can use.

movieslist.forEach(function(movie, index){
   movie.rank = index + 1;
});

You can assign certain index/rank already in forEach loop to the movieslist and then swap ranks of swapping elements to get the real ranking in compare method. See below:

const moviesListByRank = function (req, res) { 
let movieslist = [];
let movieMainRating = null;
Movie.find({}, function(err, movies){
    if(err) {
        console.log(err)
    } else {
        var tempIndex = 0;
        movies.forEach(function(movie){
            movieslist.push({
                ranking: tempIndex++,
                name: movies.name,
                category: movie.category,
                movieMainRating: ((movie.FilterAction + hobby.FilterAdventure + mo
vie.FilterRomantic)/3).toFixed(2),
                FilterAction: movie.FilterAction,
                FilterAdventure: movie.FilterAdventure,
                FilterRomatic: movie.FilterRomantic,
                _id: hobby._id
            });
        });
    }
    function compare(a,b){
        if (b.movieMainRating < a.movieMainRating) {
            var tempRank = b.ranking;
            b.ranking = a.ranking;
            a.ranking = tempRank;
            return -1;
        }
        if (b.movieMainRating > a.movieMainRating)
            return 1;
        return 0;
    }
    movieslist.sort(compare);

    res
        .status(200)
        .json(movieslist);
});
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM