Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save odykyi/7c6ef7b58eecb08c7b748f1d3ee31343 to your computer and use it in GitHub Desktop.
Save odykyi/7c6ef7b58eecb08c7b748f1d3ee31343 to your computer and use it in GitHub Desktop.
lodash-js-pagination-page-skip-sort-order-limit.js
/* PAGINATION WITH SORTING AND PAGING */
const page = 1; // input page, min value 1
const limit = 2; // input limit min value 1
/* INPUT ARRAY */
const array = [
{ Editable: true, Name: "Daniel Test", Site: "SE100"},
{ Editable: true, Name: "Test new", Site: "SE100"},
{ Editable: false, Name: "Test", Site: "SE100"},
];
/* PAGINATION WITH SORTING AND PAGING */
const result = _(array)
.orderBy(['Name'], ['asc']) // sort by ascendind
.drop((page - 1) * limit) // page in drop function starts from 0
.take(limit) // limit 2
.value();
console.log(result);
console.log(JSON.stringify(result));
/*
RESULT:
limit 2
sort by ascendind
[
{
"Editable":true,
"Name":"Daniel Test", // name sorted by ascendind
"Site":"SE100"
},
{
"Editable":false,
"Name":"Test",
"Site":"SE100"
}
]
*/
@odykyi
Copy link
Author

odykyi commented Sep 26, 2017

<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.16.6/lodash.min.js"></script>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment