[英]How to remove an element from a javascript array
I have an array as below 我有一个数组如下
var array = [];
var item1 = 'k1';
var itemvalue1 = {'key1':'value1'};
array[item1] = itemvalue1;
var item2 = 'k2';
var itemvalue2 = {'key2':'value2'};
array[item2] = itemvalue2;
Two questions... 两个问题...
Any help is appreciated. 任何帮助表示赞赏。
How to get the position(index) of the item2 in the array
如何获取item2在数组中的位置(索引)
It doesn't have one. 它没有一个。
You assigned the value to a property called k2
. 您已将该值分配给名为
k2
的属性。 That isn't a numeric index, so it doesn't have a position. 那不是数字索引,所以它没有位置。
You almost certainly should be using a plain object ( {}
) and not an array in the first place. 几乎可以肯定,您应该使用一个普通对象(
{}
),而不是一个数组。
How to remove the item item2 from the array
如何从数组中删除项目item2
delete array.k2
(as per this question ). (根据此问题 )。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.