说我有一个像这样的数组:
let arr = [1, 2, 3, 4, 5, 6, "a"]
如何将其分解为两个增量的单个变量?这样let one = [1, 2]
,let two = [3, 4]
等?我知道您可以像这样使用单个变量来解构数组:
let one, two, three;
[one, two, three] = arr
但是以两个为增量进行操作是否可行?
答案 0 :(得分:7)
您可以执行此操作的另一种方法是通过ES6 generator function:
let arr = [1, 2, 3, 4, 5, 6, 'a']
function* batch (arr, n=2) {
let index = 0
while (index < arr.length) {
yield arr.slice(index, index + n)
index += n
}
}
let [first, second, third] = batch(arr)
console.log(first)
console.log(second)
console.log(third)
或更@Patrick Roberts建议的更通用的方法:
let arr = [1, 2, 3, 4, 5, 6, 'a']
function* batch (iterable, length=2) {
let array = [];
for (const value of iterable) {
if (array.push(value) === length) {
yield array;
array = [];
}
}
if (array.length) yield array;
}
let [first, second, third] = batch(arr);
console.log(first)
console.log(second)
console.log(third)
最后是curried版本:
let arr = [1, 2, 3, 4, 5, 6, 'a']
const batch = length => function* (iterable) {
let array = [];
for (const value of iterable) {
if (array.push(value) === length) {
yield array;
array = [];
}
}
if (array.length) yield array;
}
let [first, second, third] = batch(2)(arr);
console.log(first)
console.log(second)
console.log(third)
答案 1 :(得分:4)
您可以使用一个数组,并将值分配给显式目标。
在左侧使用长度为2的数组的扩展语法无法实现自动分配。
let array = [1, 2, 3, 4, 5, 6, "a"],
one = [], two = [], three = [];
[one[0], one[1], two[0], two[1], three[0], three[1]] = array;
console.log(one);
console.log(two);
console.log(three);
答案 2 :(得分:3)
.flatMap()
是.map()
和.flat()
的组合,它允许回调跳过返回值,返回值以及返回多个值,这可以通过返回扁平化的数组来实现在最后一步。这个演示有一个回调,它是一个三元组,每隔一个迭代将在数组中返回2个值,而在交替迭代中将返回一个空数组。结果是一个数组数组,然后将其解构。
let arr = [1, 2, 3, 4, 5, 6, "a"];
let newArr = arr.flatMap((node, index, array) => index % 2 !== 0 ? [
[array[index - 1], node]
] : []);
let [A, B, C] = newArr;
console.log(A);
console.log(B);
console.log(C);
答案 3 :(得分:2)
您可以按照以下步骤进行操作:
reduce()
并将累加器设置为[]
2
)整除。
let arr = [1, 2, 3, 4, 5, 6, "a"];
let [first,second,third] = arr.reduce((ac,a,i) => {
if(i % 2 === 0){
ac.push([]);
}
ac[ac.length - 1].push(a);
return ac;
},[])
console.log(first);
console.log(second);
console.log(third);
答案 4 :(得分:0)
这里是使用curried的另一个Generator版本,但是这个版本使用Array.splice()来产生值:
let arr = [1, 2, 3, 4, 5, 6, 'a'];
const getChunk = (len) => function * (arr)
{
arr = arr.slice(0);
while (arr.length >= len)
yield arr.splice(0, len);
yield arr;
}
let [one, two, three, four] = getChunk(2)(arr);
console.log("Chunks of size 2 =>", JSON.stringify([one, two, three, four]));
[one, two, three, four] = getChunk(3)(arr);
console.log("Chunks of size 3 =>", JSON.stringify([one, two, three, four]));
[one, two, three, four] = getChunk(1)(arr);
console.log("Chunks of size 1 =>", JSON.stringify([one, two, three, four]));
.as-console {background-color:black !important; color:lime;}
.as-console-wrapper {max-height:100% !important; top:0;}
答案 5 :(得分:-2)
有可能吗?
编码几乎可以解决任何问题。
let group = (arr, count) =>
arr.reduce((acc, v) => {
let last = acc[acc.length - 1];
if (last.length < count)
last.push(v);
else
acc.push([v]);
return acc;
}, [[]]);
let arr = [1, 2, 3, 4, 5, 6, "a"]
console.log(group(arr, 2));
console.log(group(arr, 3));
console.log(group(arr, 5));