[英]Splice value from an array and push spliced value into another array
Hello and here is my problem.I have a task,i need to remove elements from an array and push them into another array and then return the array with removed elements. 您好,这是我的问题。我有一个任务,我需要从数组中删除元素,然后将它们推入另一个数组,然后返回带有删除元素的数组。 For example, in this case i have to remove all 0 values, and push them into 'a' array and then return 'a' array with there 0 values. 例如,在这种情况下,我必须删除所有0值,并将它们推入'a'数组,然后返回具有0个值的'a'数组。 I removed 0 values from the array by using splice method and loop, but i don't realy know how to push removed elemts into 'a' array, i've tried to use push method but i does not work for me. 我通过使用splice方法和循环从数组中删除了0个值,但我真的不知道如何将删除的elemts推送到'a'数组中,我试图使用push方法,但对我不起作用。 Hope you'll help me.Thank you everyone. 希望你能帮助我。谢谢大家。
function moveZeros(array) { var a = []; for (var i = array.length - 1; i--;) { if (array[i] == "0") { array.splice(i, 1); } } return a; } moveZeros([1, 2, 0, 1, 0, 1, 0, 3, 0, 1]);
Using push
should work. 使用push
应该可以。 The .splice
method will return an array with the removed elements, and you can use the spread operator ...
to pass it as a set of arguments to push
: .splice
方法将返回一个数组,其中包含已删除的元素,您可以使用spread运算符...
将其作为push
一组参数传递:
function moveZeros(array) { var a = []; for (var i = array.length - 1; i >= 0; i--) { if (array[i] == "0") { a.push(...array.splice(i, 1)); } } return a; } const array = [0, 1, 2, 0, 1, 0, 1, 0, 3, 0, 1, 0, 0]; console.log(moveZeros(array)); console.log(array)
Finally, you should put the i--
as the final part of the loop so that it only executes when each iteration finishes (instead of as they begin). 最后,应将i--
作为循环的最后一部分,以便仅在每次迭代完成时才执行(而不是在开始时)。 Then, change your condition to be i >= 0
so that you don't miss a zero at the front of the array. 然后,将条件更改为i >= 0
这样您就不会在数组的前面错过零。
Array.splice()
returns an array of removed elements, and you can use Array.concat()
to add them to the a
array. Array.splice()
返回移除元件的阵列,并且可以使用Array.concat()
将它们添加到a
数组。
Notes: 笔记:
i
to array.length
without the -1, since the condition i--
is checked before the execution of the loop's body. 将i
初始化为不带-1的array.length
,因为条件i--
在执行循环主体之前被检查。 a
to the removed elements to maintain the original order. 由于数组是从头到尾进行迭代a
,因此将a
到已删除的元素以保持原始顺序。 function moveZeros(array) { var a = []; for (var i = array.length; i--;) { if (array[i] === 0) { a = array.splice(i, 1).concat(a); } } return a; } var result = moveZeros([1, 2, 0, 1, 0, 1, 0, 3, 0, 1, 0]); // I've added a last 0 to the array to show that all items were removed console.log(result);
You could iterate from the end and splice the array if necessary. 您可以从头开始迭代,并在必要时拼接数组。 This solution mutates the array
. 此解决方案使array
变异。
function moveZeros(array) { var i = array.length, a = []; while (i--) { if (array[i] === 0) { a.push(array.splice(i, 1)[0]); } } return a; } var array = [1, 2, 0, 1, 0, 1, 0, 3, 0, 1]; console.log(moveZeros(array)); console.log(array);
.as-console-wrapper { max-height: 100% !important; top: 0; }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.