RandomUser
RandomUser

Reputation: 1853

Skip and return objects from list of object using Lodash/Underscore

I need to perform an operation similar to the following written in C#:

int[] items = { 1, 2, 3, 4, 5, 6, 7 };
var a = items.Skip(2).Take(3);

Which return 3, 4 and 5

Similarly I need to skip records from a list of object

$scope.myObject = [ { Editable: true, Name: "Daniel Test", Site: "SE100"},
                    { Editable: true, Name: "Test new", Site: "SE100"},
                    { Editable: false, Name: "Test", Site: "SE100"} ]

I need to skip first record and get back the remaining records, meaning 1-nth record

How can I do this using lodash/underscore?

Upvotes: 23

Views: 26056

Answers (5)

Gruff Bunny
Gruff Bunny

Reputation: 27976

Underscore's first and rest should do the trick:

var a = _.first( _.rest(items, 2), 3);

and rest on it's own can be used to skip the first record:

$scope.allButTheFirst = _.rest( $scope.myObject, 1)

Chaining can be used to make the statement slightly more pleasing to the eye and therefore improve transparency:

var a = _.chain(items)
    .rest(2)
    .first(3)
    .value();

As pointed out in @RhysvanderWaerden's answer, when using lodash use drop instead of first and take instead of rest.

Upvotes: 25

аlex
аlex

Reputation: 5698

/* 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"
     }
  ]
*/
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.16.6/lodash.min.js"></script>

Upvotes: 5

Skiminock
Skiminock

Reputation: 149

Having this

const skip = 0;
const take = 40;

You may simply do this

return _.slice(items, skip, skip + take);

Or just using native js

items.slice(skip, skip + take);

Upvotes: 7

Rhys van der Waerden
Rhys van der Waerden

Reputation: 3837

In Lodash the first and rest functions behave differently to Underscore. Namely they do not accept a length argument. Instead drop and take should be used:

const a = _.take(_.drop(items, skipCount), takeCount);

// or

const a = _(items).drop(skipCount).take(takeCount).value();

Upvotes: 35

Bohdan Lyzanets
Bohdan Lyzanets

Reputation: 1672

In lodash v3 You can use slice and take methods. As alternative You can use array method slice

var offset = 2;
var limit = 3;
var items = [1, 2, 3, 4, 5, 6, 7];
var result1 = _(items).slice(offset).take(limit).value();
var result2 = items.slice(offset, offset + limit);

document.getElementById('result1').innerText = result1.toString();
document.getElementById('result2').innerText = result2.toString();
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/3.7.0/lodash.min.js"></script>
<pre>
  lodash v3: <span id="result1"></span>
  vanila JS: <span id="result2"></span>
</pre>

Upvotes: 21

Related Questions