Add and remove array

 

 
1. Add
(1) The easiest way: assigned new index
(2) using the push () and unshift () method (see later push and pop)
 
2. Delete
(1) delete operator removes array elements
Delete to delete the use of elements in the array, it does not modify the array's length property, nor will shift element index down from a high filling gaps left by removing elements. The non-sparse array becomes sparse arrays.
(2) using the pop () and shuift () method
3. The method of comparing an array of learning and memory
(1) push () and pop ()
push () and pop () method allows the array as a stack (FILO) is used. Both of these methods will change the original array.
push () method to add one or more elements to the end of the array, and returns the length of the new array.
pop () method contrary, delete the last element in the array, the array length is reduced, it returns the value array element deleted.
(2)unshift()和shift()
These two methods with the above two methods are similar, but not the same place in the two methods is to insert and delete elements in the head array.
the unshift () to add one or more elements of the head array and the existing mobile element to obtain a spatial position index higher. And returns the new length of the array.
shift () Removes the first element of the array and returns. The subsequent elements down one to fill the vacancy deleted elements.
When using the unshift () method passing a plurality of parameters, new elements in the same array order as the order of transmission parameters
If the element is inserted one by one in the order, the final result will be the opposite
1. Add
(1) The easiest way: assigned new index
(2) using the push () and unshift () method (see later push and pop)
 
2. Delete
(1) delete operator removes array elements
Delete to delete the use of elements in the array, it does not modify the array's length property, nor will shift element index down from a high filling gaps left by removing elements. The non-sparse array becomes sparse arrays.
(2)使用pop()和shuift()方法
3.数组方法对比学习记忆
(1)push()和pop()
push()和pop()方法允许将数组当做栈(先进后出)来使用。这两种方法均会改变原数组。
push()方法在数组的尾部添加一个或多个元素,并返回新数组的长度。
pop()方法相反,删除数组中最后一个元素,减小数组长度,返回删除数组元素的值。
(2)unshift()和shift()
这两个方法与上面两个方法很类似,但是不一样的地方在这两个方法是在数组的头部进行元素的插入和删除操作。
unshift()在数组的头部添加一个或多个元素,并将已经存在的元素移动到更高索引的位置获得空间。并且返回数组的新长度。
shift()删除数组的第一个元素并且返回。把随后的元素下移一位来填充删除掉的空缺元素。
当使用unshift()方法传入多个参数时,数组中新增元素的顺序与传参顺序相同
如果元素是一个一个按照顺序插入,最终结果将会相反

Guess you like

Origin www.cnblogs.com/jiangquhan/p/11262113.html