如何从数组中获取前N个元素

时间:2023-01-15 13:45:03

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).

我正在使用Javascript(ES6)/ FaceBook做出反应并尝试获取大小不同的数组的前3个元素。我想做相当于Linq take(n)。

In my Jsx file I have the following:

在我的Jsx文件中,我有以下内容:

var items = list.map(i => {
  return (
    <myview item={i} key={i.id} />
  );
});

Then to get the first 3 items I tried

然后获得我尝试的前3项

  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.

这不起作用,因为地图没有设定功能。

Can you please help?

你能帮忙吗?

4 个解决方案

#1


135  

I believe what you're looking for is:

我相信你要找的是:

// ...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>   
)

#2


224  

To get the first n elements of an array, use

要获取数组的前n个元素,请使用

array.slice(0, n);

#3


6  

Do not try doing that using a map function. Map function should be used to map values from one thing to other. When the number of input and output match.

不要尝试使用地图功能。应使用Map函数将值从一个事物映射到另一个事物。当输入和输出的数量匹配时。

In this case use filter function which is also available on the array. Filter function is used when you want to selectively take values maching certain criteria. Then you can write your code like

在这种情况下,使用过滤器功能,该功能也可在阵列上使用。当您想要选择性地获取加工某些标准的值时,使用过滤器功能。然后你可以编写你的代码

var items = list
             .filter((i, index) => (index < 3))
             .map((i, index) => {
                   return (
                     <myview item={i} key={i.id} />
                   );
              });

#4


6  

This might be surprising but length property of an array is not only used to get number of array elements but it's also writable and can be used to set array's length MDN link

这可能是令人惊讶的,但数组的长度属性不仅用于获取数组元素的数量,而且它也是可写的,可用于设置数组的长度MDN链接

If current array is not needed anymore and you don't care about immutability or don't want to allocate memory i.e. for a game the fastest way is

如果不再需要当前阵列并且您不关心不变性或者不想分配内存,即对于游戏,最快的方法是

arr.length = n

to empty an array

清空一个数组

arr.length = 0

#1


135  

I believe what you're looking for is:

我相信你要找的是:

// ...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>   
)

#2


224  

To get the first n elements of an array, use

要获取数组的前n个元素,请使用

array.slice(0, n);

#3


6  

Do not try doing that using a map function. Map function should be used to map values from one thing to other. When the number of input and output match.

不要尝试使用地图功能。应使用Map函数将值从一个事物映射到另一个事物。当输入和输出的数量匹配时。

In this case use filter function which is also available on the array. Filter function is used when you want to selectively take values maching certain criteria. Then you can write your code like

在这种情况下,使用过滤器功能,该功能也可在阵列上使用。当您想要选择性地获取加工某些标准的值时,使用过滤器功能。然后你可以编写你的代码

var items = list
             .filter((i, index) => (index < 3))
             .map((i, index) => {
                   return (
                     <myview item={i} key={i.id} />
                   );
              });

#4


6  

This might be surprising but length property of an array is not only used to get number of array elements but it's also writable and can be used to set array's length MDN link

这可能是令人惊讶的,但数组的长度属性不仅用于获取数组元素的数量,而且它也是可写的,可用于设置数组的长度MDN链接

If current array is not needed anymore and you don't care about immutability or don't want to allocate memory i.e. for a game the fastest way is

如果不再需要当前阵列并且您不关心不变性或者不想分配内存,即对于游戏,最快的方法是

arr.length = n

to empty an array

清空一个数组

arr.length = 0