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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34883068/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 16:59:54  来源:igfitidea点击:

How to get first N number of elements from an array

javascriptreactjs

提问by user1526912

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 react 并尝试获取大小不同的数组的前 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?

你能帮忙吗?

回答by Oriol

To get the first nelements of an array, use

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

array.slice(0, n);

回答by Patrick Shaughnessy

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

回答by Pawel

This might be surprising but lengthproperty 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. This will mutate the array.

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

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

回答by sandeep

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} />
                   );
              });

回答by Freddy

You can filter using indexof array.

您可以使用index数组进行过滤。

var months = ['Jan', 'March', 'April', 'June'];
months = months.filter((month,idx) => idx < 2)
console.log(months);

回答by FlyingSnowGhost

The following worked for me.

以下对我有用。

array.slice( where_to_start_deleting, array.length )

Here is an example

这是一个例子

var fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.slice(2, fruits.length);
//Banana,Orange  ->These first two we get as resultant

回答by Siderite Zackwehdex

With https://github.com/Siderite/LInQeryou can to Enumerable.from(list).take(3).toArray();

使用https://github.com/Siderite/LInQer你可以 Enumerable.from(list).take(3).toArray();

回答by Omer

Use Filter

使用过滤器

Not the best practice but another way

不是最佳实践,而是另一种方式

const cutArrByN = arr.filter((item, idx) => idx < n);

const cutArrByN = arr.filter((item, idx) => idx < n);