How to get the value in the array corresponding to the even-numbered subscript
In other words: Get the array
corresponding to the even subscript in the array
Based on the above sentence, it is a reasonable guess that what you are actually talking about is Get the arrays corresponding to even subscripts from the two-dimensional array and flatten them into an array
For example
var test = [
['a'],
['b'],
['c'],
['d']
]
After processing, the result is ['a', 'c'], that is, the arrays corresponding to even subscripts are merged into one array (the subscripts start from 00 is an even number)
If you are convinced that this is the case, please continue reading
First get the arrays corresponding to even subscripts
var isEven = i => i % 2 === 0;
var evens = arr => arr.filter(
// 子数组, 序号 => idx 是偶数则返回 true 否则 false
// 这样可以过滤掉奇数下标的元素
(subArr, idx) => isEven(idx)
);
Your question
In other words: Get the
corresponding to the even subscript in the arrayarray
Based on the above sentence, it is a reasonable guess that what you are actually talking about is
Get the arrays corresponding to even subscripts from the two-dimensional array and flatten them into an array
For example
After processing, the result is
['a', 'c']
, that is, the arrays corresponding toeven subscripts are merged into one array
(the subscripts start from0
0 is an even number
)If you are convinced that this is the case, please continue reading
First get the arrays corresponding to even subscripts
Flatten the arrays obtained with evens
For example,
after processing[[1], [2]]
becomes[1, 2]
This process is paving
Assembly
Test
Define the array to be tested
The expected value is
here is array number 0, which is of course an even number
and0,1,2,3,4
The following is the test code:
ScreenShot
The result is as shown in the picture
Expected income, sure it’s feasible.
Links
Some knowledge points
MDN - filter for arrays
MDN - reduce for arrays
MDN - arrow function
Help you simply implement a function