Skip to content

Instantly share code, notes, and snippets.

@coder618
Last active April 19, 2020 04:11
Show Gist options
  • Save coder618/535a868dc798287097a28c8dd07a5dc9 to your computer and use it in GitHub Desktop.
Save coder618/535a868dc798287097a28c8dd07a5dc9 to your computer and use it in GitHub Desktop.
/**
* This function will return array start and end index
* which can be use in array slice Method
* @param {int} pageNumber
* @param {int} posts_per_page
* Logic Behind it
* 5
0 0 5
1 5 10
2 10 15
3 15 20
arr.slice(0,5)
(5) [1, 2, 3, 4, 5]
arr.slice(5,10)
(5) [6, 7, 8, 9, 10]
arr.slice(10,15)
(5) [11, 12, 13, 14, 15]
arr.slice(15,20)
(5) [16, 17, 18, 19, 20]
*/
returnStartEndIndex(pageNumber, posts_per_page) {
pageNumber = pageNumber || this.state.currentPage;
posts_per_page = posts_per_page || this.state.posts_per_page;
let startIndex = posts_per_page * pageNumber;
let endIndex = startIndex + posts_per_page;
return [ startIndex, endIndex ];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment