与JavaScript的push()相反的是什么?方法?

假设我有一个数组:

var exampleArray = ['remove'];

我想要push();“keep”这个词

exampleArray.push('keep');

我如何删除字符串'删除'从数组?


当前回答

Push()在结束时添加;Pop()从end删除。

Unshift()添加到front;Shift()从前面删除。

Splice()可以在任何地方做它想做的任何事情。

其他回答

你问了两个问题。与push()(问题的标题)相反的是pop()。

var exampleArray = ['myName']; exampleArray.push('嗨'); console.log (exampleArray); exampleArray.pop (); console.log (exampleArray);

pop()将从exampleArray中删除最后一个元素并返回该元素("hi"),但它不会从数组中删除字符串"myName",因为"myName"不是最后一个元素。

你需要的是shift()或splice():

var exampleArray = ['myName']; exampleArray.push('嗨'); console.log (exampleArray); exampleArray.shift (); console.log (exampleArray);

var exampleArray = ['myName']; exampleArray.push('嗨'); console.log (exampleArray); exampleArray。拼接(0,1); console.log (exampleArray);

有关更多数组方法,请参见:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array#Mutator_methods

Push()在结束时添加;Pop()从end删除。

Unshift()添加到front;Shift()从前面删除。

Splice()可以在任何地方做它想做的任何事情。