Is it possible to map only a portion of an array? (Array.map())

前端 未结 8 979
终归单人心
终归单人心 2020-12-31 07:16

I am building a project using React.js as a front-end framework. On one particular page I am displaying a full data set to the user. I have an Array which contains this full

8条回答
  •  感动是毒
    2020-12-31 07:33

    There is no version of the map() function that only maps a partial of the array.

    You could use .map() in conjunction with .filter().

    You get the index of the current element as the second arg of map and if you have a variable for current page and page size you can quite easily filter the right page from your array without having to really slice it up.

    var currentPage = 1;
    var pageSize = 25;
    
    dataArray.filter(function(elt, index) {
    
        var upperThreshold = currentPage * pageSize;
        var lowerThreshold = currentPage * pageSize - pageSize;
    
        return index < upperThreshold && index > lowerThreshold;
    
    });
    

提交回复
热议问题