javascript 使用 Lodash/Underscore 从对象列表中跳过并返回对象
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26801031/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Skip and return objects from list of object using Lodash/Underscore
提问by RandomUser
I need to perform an operation similar to the following written in C#:
我需要执行类似于以下用 C# 编写的操作:
int[] items = { 1, 2, 3, 4, 5, 6, 7 };
var a = items.Skip(2).Take(3);
Which return 3, 4 and 5
返回3、4 和 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 recordand get back the remaining records, meaning 1-nthrecord
我需要跳过第一条记录并取回剩余的记录,即第1-n 条记录
How can I do this using lodash/underscore?
我如何使用 lodash/下划线来做到这一点?
回答by Gruff Bunny
Underscore's firstand restshould do the trick:
Underscore 的first和rest应该可以解决问题:
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
.
正如@RhysvanderWaerden 的回答中所指出的,在使用 lodash 时,使用drop
代替first
和take
代替rest
.
回答by Rhys van der Waerden
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:
在 Lodash 中,first
和rest
函数的行为与Underscore不同。即他们不接受长度参数。相反drop
,take
应该使用:
const a = _.take(_.drop(items, skipCount), takeCount);
// or
const a = _(items).drop(skipCount).take(takeCount).value();
回答by Bohdan Lyzanets
In lodash v3You can use sliceand takemethods. As alternative You can use array method slice
在lodash v3 中可以使用slice和take方法。作为替代,您可以使用数组方法切片
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>
回答by Skiminock
Having this
有这个
const skip = 0;
const take = 40;
You may simply do this
你可以简单地这样做
return _.slice(items, skip, skip + take);
Or just using native js
或者只是使用原生js
items.slice(skip, skip + take);
回答by аlex dyky?
/* 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>
回答by Yogesh Patel
You can use _.chunk(array,count)
.
您可以使用_.chunk(array,count)
.
Just pass count you will get all array devided in count size array then pass value and you rock.+1
只需通过计数,您就会将所有数组划分为计数大小数组,然后传递值,然后您就会摇滚。+1