简体   繁体   English

使用splice和for循环删除原始数组

[英]get removed and original array using splice and for loop

I'm trying to create multiple arrays from one array and then add html strings. 我正在尝试从一个数组创建多个数组,然后添加html字符串。 I use .splice() and for . 我使用.splice()for My main problem is that .splice() only shows the removed arrays and not the first. 我的主要问题是.splice()只显示删除的数组而不是第一个。

Here's what I have so far: 这是我到目前为止所拥有的:

array = ["a1", "a2", "a3", "b1", "b2", "b3", "c1", "c2" ,"c3","d1","d2","d3"]

for(var i = 0; i < array.length; i++){
array.splice(0,3);
console.log(array[0] + '<br/>' + '<a href="' + array[2] + '">' + array[1] + '</a>');

 }

The only problem is that this results in: 唯一的问题是,这导致:

b1<br/><a href="b3">b2</a>
c1<br/><a href="c3">c2</a>
d1<br/><a href="d3">d2</a>

I'm missing the first array which contains the 'a' values. 我错过了第一个包含'a'值的数组。 What is the best way to output all these arrays? 输出所有这些数组的最佳方法是什么?

Just do something like this? 做这样的事情?

for(var i = 0; i < array.length; i+=3){
   console.log(array[i] + '<br/>' + '<a href="' + array[i+2] + '">' + array[i+1] + '</a>');
}

jsFiddle 的jsfiddle

Or you could just move the splice line to the end of the loop. 或者您可以将拼接线移动到循环的末尾。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在Javascript中使用拼接方法获取未删除的数组元素 - how to get the non-removed array element on using splice method in Javascript 返回没有删除元素的数组? 在不更改数组的情况下使用 splice()? - Returning an array without a removed element? Using splice() without changing the array? 使用for循环从数组中拼接多个元素 - splice multiple elements from an array using for loop 使用循环从带有拼接的数组中选择项 - select items from array with splice using a loop 在 for 循环中使用 array.splice 时发生无限循环 - infinite loop occurs when using array.splice inside a for loop JS .splice不单独返回数组中已删除的项目 - JS .splice returns removed items in array not by itself jQuery-使用拼接删除数组中的项目,但返回数组中剩余的项目,而不是删除的项目 - jQuery - remove items in array using splice, but return array with remaining items, not with the removed item 如果我在JavaScript中拼接克隆的数组,为什么我的原始数组会被拼接? - Why does my original array get spliced if I splice the cloned array in JavaScript? 使用splice方法无限循环JavaScript遍历数组 - Looping through array using splice method infinite loop javascript 使用for循环拼接数组的正确方法是什么,嵌套了if条件 - what is the correct way to splice an array using for loop, having nested if conditions
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM