如何从数组中获取前N个元素 [英] How to get first N number of elements from an array
本文介绍了如何从数组中获取前N个元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用Javascript(ES6)/ FaceBook做出反应并尝试获取大小不同的数组的前3个元素。我想做相当于Linq take(n)。
I am working with Javascript(ES6) /FaceBook react and trying to get the first 3 elements of an array that varies in size. I would like do the equivalent of Linq take(n).
在我的Jsx文件中,我有以下内容:
In my Jsx file I have the following:
var items = list.map(i => {
return (
<myview item={i} key={i.id} />
);
});
然后获取我尝试过的前3个项目
Then to get the first 3 items I tried
var map = new Map(list);
map.size = 3;
var items = map(i => {
return (<SpotlightLandingGlobalInboxItem item={i} key={i.id} />);
});
这不起作用,因为地图没有设定功能。
This didn't work as map doesn't have a set function.
你能帮忙吗?
推荐答案
我相信你所寻找的是:
// ...inside the render() function
var size = 3;
var items = list.slice(0, size).map(i => {
return <myview item={i} key={i.id} />
}
return (
<div>
{items}
</div>
)
这篇关于如何从数组中获取前N个元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文