如何从数组中获取前 N 个元素 [英] How to get first N number of elements from an array

查看:45
本文介绍了如何从数组中获取前 N 个元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Javascript(ES6)/FaceBook react 并尝试获取大小不同的数组的前 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} />);
    });

这不起作用,因为地图没有设置功能.<​​/p>

你能帮忙吗?

This didn't work as map doesn't have a set function.

推荐答案

我相信您正在寻找的是:

解决方案
I believe what you're looking for is: 

)

这篇关于如何从数组中获取前 N 个元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆