Returns a range of values ​​from an array in underscore.js

I have an array with the following data

var a = [1,2,3,4,5,6,7] 

I am looking for a method in underscore.js or backbone.js in which I can return elements with the specified range. for ex:

 filter(2,5) should return [3,4,5,6] 

which is the second to fifth index element in the array. Any pointers for me?

+6
source share
2 answers

A Javascript array must be defined as follows:

 var a = [1,2,3,4,5,6,7]; //not inside {} 

And then you can use your own slice array to get elements from a specific position

 a.slice(2, 6) //should return 3,4,5,6 

Edit:

I know very well that functionality is available in JScript. I asked if this is available in the spine or underlining. You ask why you need an ice cube instead of water, because ice will eventually turn into water.

Underscore js do not have a function like slice , as it is already available in native js.

+30
source

Step 1:

Switch to Lodash. ( https://lodash.com/ )

Note: a dangerous step, you will never return.

Step 2:

Use the _.slice function as follows:

_.slice(a, 2, 5)

+1
source

Source: https://habr.com/ru/post/912870/


All Articles