如何过滤数字数组并采取一定的间隔?
How to filter an array of numbers and take certain intervals?
我有这种数组。
[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24]
过滤数组的要求是
- 每 4 项省略一次
- 取以下4件
并继续直到数组的末尾。
最后应该是return[5,6,7,8,13,14,15,16,21,22,23,24]
const values = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24];
newValues = values.filter((v, i) => i % 8 > 3)
console.log(newValues)
您可以将块切片并添加到结果数组。
const
data = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24],
result = [];
let i = 0;
while (i < data.length) result.push(...data.slice(i += 4, i += 4));
console.log(...result);
您可以创建一个分区函数来实现您的结果。
const arr = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24];
/**
* Pertition the array and create a new array.
*
* @param {array} arr The array to pertition.
* @param {number} skip The number of elements to skip
* @param {number} take Take the number of elements need to take after skipping.
*
* @return {array} The resulting array after pertition.
*/
const arrayAfterPertition = (arr, skip, take) => {
const length = arr.length;
let ans = [];
/** If there are not enough elements to skip then returns empty array */
if (length <= skip) return [];
let takeIndex = skip;
while (takeIndex < length) {
ans = [...ans, ...arr.slice(takeIndex, takeIndex + take)];
takeIndex = takeIndex + take + skip;
}
return ans;
}
console.log(JSON.stringify(arrayAfterPertition(arr, 4, 4)));
.as-console-wrapper {min-height: 100%!important; top: 0}
Lodash 如果你不介意的话。使用 Loash#chunk
const nums = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24];
const chunkSize = 4;
const result = _.chunk(nums, chunkSize)
.filter((v,i) => i % 2 ===1)
.flat();
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
<script src="https://cdn.jsdelivr.net/lodash/4.10.0/lodash.js"></script>
我有这种数组。
[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24]
过滤数组的要求是
- 每 4 项省略一次
- 取以下4件
并继续直到数组的末尾。
最后应该是return[5,6,7,8,13,14,15,16,21,22,23,24]
const values = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24];
newValues = values.filter((v, i) => i % 8 > 3)
console.log(newValues)
您可以将块切片并添加到结果数组。
const
data = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24],
result = [];
let i = 0;
while (i < data.length) result.push(...data.slice(i += 4, i += 4));
console.log(...result);
您可以创建一个分区函数来实现您的结果。
const arr = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24];
/**
* Pertition the array and create a new array.
*
* @param {array} arr The array to pertition.
* @param {number} skip The number of elements to skip
* @param {number} take Take the number of elements need to take after skipping.
*
* @return {array} The resulting array after pertition.
*/
const arrayAfterPertition = (arr, skip, take) => {
const length = arr.length;
let ans = [];
/** If there are not enough elements to skip then returns empty array */
if (length <= skip) return [];
let takeIndex = skip;
while (takeIndex < length) {
ans = [...ans, ...arr.slice(takeIndex, takeIndex + take)];
takeIndex = takeIndex + take + skip;
}
return ans;
}
console.log(JSON.stringify(arrayAfterPertition(arr, 4, 4)));
.as-console-wrapper {min-height: 100%!important; top: 0}
Lodash 如果你不介意的话。使用 Loash#chunk
const nums = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24];
const chunkSize = 4;
const result = _.chunk(nums, chunkSize)
.filter((v,i) => i % 2 ===1)
.flat();
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
<script src="https://cdn.jsdelivr.net/lodash/4.10.0/lodash.js"></script>