从提供的数组创建具有确定长度而不以 0 开头的新数组的好方法
Nice way to create new arrays from provided array with determined length without start with 0
我想我实现了我的目标,但我确信这不是实现目标的最佳方法。
我有一个函数,但有一个问题导致它添加了一些额外的 [0],我知道这是因为 while 测试继续进行。我不需要用 while+splice 来做。我想要一些建议以使其更容易。我的目标是从提供的数组开始,始终从不同于 0 的元素开始创建新数组,长度将提供为 k:
function splitNumber(arrayProvided, k) {
let newArray = [];
while (arrayProvided.length > 0) {
newArray.push(
arrayProvided.splice(
arrayProvided.findIndex(el => el),
k
)
);
}
return newArray;
}
console.log(
splitNumber([1, 0, 4, 6, 0, 7, 8, 4, 2, 0, 8, 3, 0, 0, 0, 0, 0, 0, 0, 6], 4)
);
此代码的结果是:
[
[ 1, 0 ], [ 4, 6 ],
[ 7, 8 ], [ 4, 2 ],
[ 8, 3 ], [ 6 ],
[ 0 ], [ 0 ],
[ 0 ], [ 0 ],
[ 0 ], [ 0 ],
[ 0 ], [ 0 ],
[ 0 ]
]
它是正确的,部分原因是系统在完成添加额外 [0] 的工作后有额外的工作。系统不能在第一个数组位置以 0 值开始,也不需要额外的 [0](这是因为逻辑不完全正确),是的,新数组的长度是 k 值。
没有零,您可以添加另一个检查并省略不需要的零。
这种方法不会改变给定的数据。
function splitNumber(array, k) {
let result = [],
i = 0;
while (i < array.length) {
if (array[i] === 0) {
i++;
continue;
}
result.push(array.slice(i, i += k));
}
return result;
}
console.log(splitNumber([1, 0, 4, 6, 0, 7, 8, 4, 2, 0, 8, 3, 0, 0, 0, 0, 0, 0, 0, 6], 4));
我认为使用 findIndex
隐式检查非零索引已经是一个非常聪明的解决方案。但是,当它 returns -1
时,您需要处理,就像没有找到非零条目的情况一样。因此,进行检查可以解决您的问题。
function splitNumber(arrayProvided, k) {
let newArray = [];
while (arrayProvided.length > 0) {
let nonZeroStartIndex = arrayProvided.findIndex(el => el )
if( nonZeroStartIndex == -1 ){
break;
}
else{
newArray.push(
arrayProvided.splice( nonZeroStartIndex , k )
);
}
}
return newArray;
}
console.log(
splitNumber([1, 0, 4, 6, 0, 7, 8, 4, 2, 0, 8, 3, 0, 0, 0, 0, 0, 0, 0, 6], 4)
);
然后,可以将检查移至 while
循环,让它在找不到更多非零条目时优雅地退出
function splitNumber(arrayProvided, k) {
let newArray = [];
let nonZeroStartIndex = arrayProvided.findIndex(el => el )
while (nonZeroStartIndex != -1) {
newArray.push( arrayProvided.splice( nonZeroStartIndex, k ) );
nonZeroStartIndex = arrayProvided.findIndex(el => el )
}
return newArray;
}
console.log(
splitNumber([1, 0, 4, 6, 0, 7, 8, 4, 2, 0, 8, 3, 0, 0, 0, 0, 0, 0, 0, 6], 4)
);
我想我实现了我的目标,但我确信这不是实现目标的最佳方法。 我有一个函数,但有一个问题导致它添加了一些额外的 [0],我知道这是因为 while 测试继续进行。我不需要用 while+splice 来做。我想要一些建议以使其更容易。我的目标是从提供的数组开始,始终从不同于 0 的元素开始创建新数组,长度将提供为 k:
function splitNumber(arrayProvided, k) {
let newArray = [];
while (arrayProvided.length > 0) {
newArray.push(
arrayProvided.splice(
arrayProvided.findIndex(el => el),
k
)
);
}
return newArray;
}
console.log(
splitNumber([1, 0, 4, 6, 0, 7, 8, 4, 2, 0, 8, 3, 0, 0, 0, 0, 0, 0, 0, 6], 4)
);
此代码的结果是:
[
[ 1, 0 ], [ 4, 6 ],
[ 7, 8 ], [ 4, 2 ],
[ 8, 3 ], [ 6 ],
[ 0 ], [ 0 ],
[ 0 ], [ 0 ],
[ 0 ], [ 0 ],
[ 0 ], [ 0 ],
[ 0 ]
]
它是正确的,部分原因是系统在完成添加额外 [0] 的工作后有额外的工作。系统不能在第一个数组位置以 0 值开始,也不需要额外的 [0](这是因为逻辑不完全正确),是的,新数组的长度是 k 值。
没有零,您可以添加另一个检查并省略不需要的零。
这种方法不会改变给定的数据。
function splitNumber(array, k) {
let result = [],
i = 0;
while (i < array.length) {
if (array[i] === 0) {
i++;
continue;
}
result.push(array.slice(i, i += k));
}
return result;
}
console.log(splitNumber([1, 0, 4, 6, 0, 7, 8, 4, 2, 0, 8, 3, 0, 0, 0, 0, 0, 0, 0, 6], 4));
我认为使用 findIndex
隐式检查非零索引已经是一个非常聪明的解决方案。但是,当它 returns -1
时,您需要处理,就像没有找到非零条目的情况一样。因此,进行检查可以解决您的问题。
function splitNumber(arrayProvided, k) {
let newArray = [];
while (arrayProvided.length > 0) {
let nonZeroStartIndex = arrayProvided.findIndex(el => el )
if( nonZeroStartIndex == -1 ){
break;
}
else{
newArray.push(
arrayProvided.splice( nonZeroStartIndex , k )
);
}
}
return newArray;
}
console.log(
splitNumber([1, 0, 4, 6, 0, 7, 8, 4, 2, 0, 8, 3, 0, 0, 0, 0, 0, 0, 0, 6], 4)
);
然后,可以将检查移至 while
循环,让它在找不到更多非零条目时优雅地退出
function splitNumber(arrayProvided, k) {
let newArray = [];
let nonZeroStartIndex = arrayProvided.findIndex(el => el )
while (nonZeroStartIndex != -1) {
newArray.push( arrayProvided.splice( nonZeroStartIndex, k ) );
nonZeroStartIndex = arrayProvided.findIndex(el => el )
}
return newArray;
}
console.log(
splitNumber([1, 0, 4, 6, 0, 7, 8, 4, 2, 0, 8, 3, 0, 0, 0, 0, 0, 0, 0, 6], 4)
);