使用Javascript - 弹出从数组的值,而不是在数组的末尾 [英] Javascript - Pop a value from array, but not at the end of array
问题描述
我有例如,这阵列(每个数字都是singolar,没有一个重复的)被称为P值:1 2 3 15 20 12 14 18 7 8(sizeof的10)
我需要例如弹出值15,而这个P值后,应该是1 2 3 20 12 14 18 7 8(sizeof的9)。该怎么办呢?
在弹出()函数取值在数组的末尾。我不希望这:)
欢呼声
修改
为(i = 0; I< pvalue.length;我++){
如果(P值[I] ==参数){
IND = I;
打破;
}
}
pvalue.splice(IND,1);
您正在寻找的<$c$c>splice$c$c>.例如: http://jsbin.com/oteme3 :
变种A,B;一个= [1,2,3,15,20,12,14,18,7,8]
显示(则为a.length之前=+则为a.length);
B = a.splice(3,1);
显示(之后则为a.length =+则为a.length);
显示器(B [0] =+ B [0]);
...显示则为a.length之前= 10,然后在则为a.length后= 9,然后B [0] = 15
注意拼接
返回被移除的值,而不是只是一个一个的阵列的,但是这很容易处理。这也方便了插入值到的数组。
I have for example this array (each number is singolar, no one duplicate) called pvalue : 1 2 3 15 20 12 14 18 7 8 (sizeof 10).
I need for example to pop the value "15", and after this pvalue should be 1 2 3 20 12 14 18 7 8 (sizeof 9). How can do it?
the pop() function take the value at the end of the array. I don't want this :) cheers
EDIT
for(i=0; i<pvalue.length; i++) {
if(pvalue[i]==param) {
ind=i;
break;
}
}
pvalue.splice(ind, 1);
You're looking for splice
. Example: http://jsbin.com/oteme3:
var a, b;
a = [1, 2, 3, 15, 20, 12, 14, 18, 7, 8];
display("a.length before = " + a.length);
b = a.splice(3, 1);
display("a.length after = " + a.length);
display("b[0] = " + b[0]);
...displays "a.length before = 10", then "a.length after = 9", then "b[0] = 15"
Note that splice
returns an array of the removed values rather than just one, but that's easily handled. It's also convenient for inserting values into an array.
这篇关于使用Javascript - 弹出从数组的值,而不是在数组的末尾的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!