简体   繁体   English

从 JavaScript 数组中获取接下来的两个元素

[英]Get the next two proceeding elements from a JavaScript array

I have a javascript array And i wish to get the next preceeding two elements using a function and slice.我有一个 javascript 数组,我希望使用函数和切片获取下一个前面的两个元素。 I'm not getting the result.我没有得到结果。 Is there any way to go around this?有什么办法可以解决这个问题吗?

 var arr = [1,2,3,4,5,6,7,8,9]

 function get(arr){
    for(var i=0; i<arr.length; i++){
       console.log(arr.slice(i, 3))
    }}


// Now when I call the function

    get(arr)

// Funny output

[1, 2, 3]
[2, 3]
[3]
[]
[]
[]
[]
[]
[]
[]
[]

You need the index instead of the length with Array#slice您需要使用Array#slice的索引而不是长度

Syntax句法

arr.slice([begin[, end]])

... ...

end Optional end可选

Zero-based index before which to end extraction.从零开始的索引,在此之前结束提取。 slice extracts up to but not including end. slice提取到但不包括结尾。

For example, slice(1,4) extracts the second element through the fourth element (elements indexed 1, 2, and 3).例如, slice(1,4)提取第二个元素到第四个元素(索引为 1、2 和 3 的元素)。

A negative index can be used, indicating an offset from the end of the sequence.可以使用负索引,指示距序列末尾的偏移量。 slice(2,-1) extracts the third element through the second-to-last element in the sequence. slice(2,-1)通过序列中的倒数第二个元素提取第三个元素。

If end is omitted, slice extracts through the end of the sequence ( arr.length ).如果省略endslice从序列的末尾( arr.length )提取。

If end is greater than the length of the sequence, slice extracts through to the end of the sequence ( arr.length ).如果end大于序列的长度, slice提取到序列的末尾 ( arr.length )。

 function get(arr) { for (var i = 0; i < arr.length; i++) { console.log(arr.slice(i, i + 3)); } } var arr = [1, 2, 3, 4, 5, 6, 7, 8, 9]; get(arr);

The second parameter to slice() is end index. slice()的第二个参数是结束索引。 So what you're saying is give me all elements from i to index 3 (not-inclusive).所以你说的是给我从i到索引 3 (不包括在内)的所有元素。

What you should be saying is slice(i, i+3)你应该说的是slice(i, i+3)

slice(i, 3) basically means you want a new array with elements between the index before index i and index 3 . slice(i, 3)基本上意味着您想要一个新数组,其中的元素位于索引i和索引3之前的索引之间。 You can just use a simple for loop instead to return the proceeding two elements of each element like this:您可以只使用一个简单的for loop来返回每个元素的后续两个元素,如下所示:

 var arr = [1,2,3,4,5,6,7,8,9] function get(x){ for (i = 0; i < x.length - 2; i++){ console.log("The proceeding two elements of " + x[i] + " is " + x[i+1] + " and " + x[i+2]); } } get(arr);

NB The - 2 in the for loop's x.length - 2 is to prevent returning an undefined result since the last two elements won't have two proceeding elements. NB- 2在for循环的x.length - 2是防止返回一个不确定的结果,因为最后两个元素不会有两个程序的元素。


Or if you prefer using slice() , you can do this:或者,如果您更喜欢使用slice() ,您可以这样做:

 var arr = [1,2,3,4,5,6,7,8,9] function get(x){ for (i = 0; i < x.length - 2; i++){ console.log(x.slice(i, i+3)); } } get(arr);

Again, the - 2 in the for loop's x.length - 2 is to prevent returning an undefined result or an incomplete result since the last two elements won't have two proceeding elements.再次, - 2在for循环的x.length - 2是防止返回一个未定义的结果或不完整的结果,因为最后两个元素不会有两个程序的元素。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM