Some methods commonly used to update array

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/weixin_42301962/article/details/102753452

1. push () method to add one or more elements to the end of the array, and returns the new length

var arr = ['apple','orange','pear'];
console.log("原数组:"+arr);
console.log(arr.push("banana"));
console.log("新数组:"+arr);

Output Results:
Here Insert Picture Description
2. POP () This method is used to remove and return the last element of the array

var arr = ['apple','orange','pear'];
console.log("原数组:"+arr);
console.log(arr.pop());
console.log("新数组:"+arr);

Output Results:
Here Insert Picture Description
3. Shift () value of the method used to remove the first element in the array and return the removed element

var arr = ['apple','orange','pear'];
console.log("原数组:"+arr);
console.log(arr.shift());
console.log("新数组:"+arr);

Output Results:
Here Insert Picture Description
4. the unshift () method to add one or more elements to the beginning of an array, and returns the new length

var arr = ['apple','orange','pear'];
console.log("原数组:"+arr);
console.log(arr.unshift("banana"));
console.log("新数组:"+arr);

Output Results:
Here Insert Picture Description
5. The splice () method to / add / delete items from the array, and returns the item that was removed

arrayObject.splice(index,howmany,item1,…,itemX)

  • index Required. Integer, regulations add / remove items of location, may require the use of a negative position from the end of the array.
  • howmany necessary. The number of items to delete. If set to 0, it will not remove items.
  • item, ..., itemX optional. New projects added to the array.

Add Item

var arr = ['apple','orange','pear'];
console.log("原数组:"+arr);
console.log(arr.splice(2,0,"banana"));
console.log("新数组:"+arr);

Output:
Here Insert Picture Description
replace deleted items, delete items returned

var arr = ['apple','orange','pear'];
console.log("原数组:"+arr);
console.log(arr.splice(2,1,"banana"));
console.log("新数组:"+arr);

Output:
Here Insert Picture Description
Remove items

var arr = ['apple','orange','pear'];
console.log("原数组:"+arr);
console.log(arr.splice(1,1));
console.log("新数组:"+arr);

Output:
Here Insert Picture Description
6. The Sort () This method is used to sort the elements of the array

var arr = ['apple','orange','pear','banana'];
console.log("原数组:"+arr);
console.log("新数组:"+arr.sort());

Output:
Here Insert Picture Description
digital Sort

function sortNumber(a,b)
{
	return a - b
}
var arr = ['123','34','66','6','888','9'];
console.log("原数组:"+arr);
console.log("新数组:"+arr.sort(sortNumber));

Output Results:
Here Insert Picture Description
7. The Reverse () which inverts the order of elements in the array

var arr = ['apple','orange','pear','banana'];
console.log("原数组:"+arr);
console.log("新数组:"+arr.reverse());

Output:
Here Insert Picture Description
8. The filter () method creates a new array, a new array element in the array is designated by checking all elements meet the conditions.

9. concat The method for connecting two or more arrays. This method does not change the existing array, but only returns a copy of the array is connected.

var arr = ['apple','orange','pear'];
console.log("原数组:"+arr);
console.log("新数组:"+arr.concat('banana'));

Output:
Here Insert Picture Description

var arr = ['apple','orange','pear'];
var arr1 = [1,2];
console.log("原数组:"+arr);
console.log("新数组:"+arr.concat(arr1));

Output Results:
Here Insert Picture Description
10. The Slice This method returns the selected elements from the array prior

var arr = ['apple','orange','pear'];
console.log("原数组:"+arr);
console.log("新数组:"+arr.slice(0,1));

Output:
Here Insert Picture Description

Guess you like

Origin blog.csdn.net/weixin_42301962/article/details/102753452