如何从javascript变量中删除对象/数组?

时间:2015-04-29 16:21:49

标签: javascript

如何从javascript变量中删除对象/数组?

1 个答案:

答案 0 :(得分:1)

至于W3CSchool http://www.w3schools.com/jsref/jsref_pop.asp

您可以使用 .pop() 方法。

var fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.pop();

结果将是:

Banana,Orange,Apple

删除其他元素

要删除数组内索引处的某个元素,可以使用方法splice

所以例如

var myFish = ['angel', 'clown', 'drum', 'surgeon', 'apple'];
// removes 1 element from index 3
removed = myFish.splice(3, 1);

结果将是

myFish is ['angel', 'clown', 'drum', 'apple']
removed is ['surgeon']

请记住,数组是零索引的,因此第3个元素是元素编号2.