JavaScript Reference Types Detailed -- Exploring Array Types (Slice, Location Index, Iteration, Merge)

Keywords: less

1. Operating method 2: slice(), splice()

(1) Slicing: slice() method, which creates a new array based on one or more items of the current array, can accept one or two parameters, that is, the starting and ending positions of the items to be returned.
One parameter: Returns all items from the specified position of the parameter to the end of the current array
Two parameters: Returns the item between the start position, but does not include the item at the end position. The slice method does not affect the original array.
For example:

var colors = ["red","orange","yellow","green","blue","purple"];
console.log(colors);	//red,orange,yellow,green,blue,purple
var colors2 = colors.slice(1);
var colors3 = colors.slice(1,4);
console.log(colors2);	//orange,yellow,green,blue,purple
console.log(colors3);	//orange,yellow,green);

If the parameters in the slice() method contain negative numbers, the number plus the length of the array is used to determine the corresponding number of digits.
If the end position is less than the start position, the empty array is returned.
For example:

var colors = ["red","orange","yellow","green","blue","purple"];
var colors2 = colors.slice(-1);
var colors3 = colors.slice(-3,-1);
console.log(colors2);	//purple
console.log(colors3);	//green,blue

(2) splice() method: arrays can be deleted, replaced, and added

  • Delete: You can delete any number of items, specify two parameters, the first item to be deleted and the number of items to be deleted.
    colors.splice(0,2);,
    Delete the first two items of the array.
  • Insert: You can insert any number of items to a specified location, providing three parameters: the starting position, 0 (the number of items to be deleted) and the item to be inserted. If you insert more than one item, you can pass in the fourth and fifth items
    colors.splice(2,0,"pink");
    Insert "pink" after the second item of the array
  • Replacement: You can insert any number of items to the specified location, specifying three parameters: the starting position, the number of items to be deleted, any number of items to be inserted, insertion and deletion items need not be equal.
    colors.splice(0,2,"pink","brown");
    Delete the first two items of the array and insert "pink" and "brown" from the first item
2. Location methods: indexOf() and lastIndexOf() methods

Two parameters, the item to be looked up and the index representing the location of the starting point of the search (optional)
indexOf() lookup from front to back
lastIndexOf() Looks backward and forward

Both of them return the position of the item to be searched in the array, but - 1 is not found.
For example:·

var  nums = [1,2,3,4,5,6,7,4,8,9,10];
console.log(nums.indexOf(4));	//3
console.log(nums.lastIndexOf(4));	//7
console.log(nums.indexOf(4,5);	//7
3. Iterative method

Five, each method receives two parameters: the function running on each and the scope object running the function - affecting this (optional), without affecting the original array.
var nums = [1,2,3,4,5,6,7,4,8,9,10];

  • every(): Runs a given function for each item of the array, and returns true if the function returns true for each item.
  • some(): Run a given function for each item in the array, and return true if the function returns true for any item.
  • filter(): Runs a given function for each item in an array, returning an array of true items.
  • forEach(): Runs the given function for each item in the array without a return value.
  • map(): Run a given function for each item in the array, returning an array of the results of each function call.
var everyResult = nums.every(function(item,index,array){
	return (item>4);
});
console.log(everyResult);	//false

var someResult = nums.some(function(item,index,array){
	return (item>4);
});
console.log(someResult);	//true

var filterResult = nums.filter(function(item,index,array){
	return (item>4);
});
console.log(filterResult);	//5,6,7,8,9,10

var mapResult = nums.map(fucntion(item,index,array){
	return (item*2);
});
console.log(mapResult);	//2,4,6,8,10,12,14,8,16,18,20

var foreachResult = nums.forEach()item,index,array){
	console.log(item*2);
});
4. Merge method: reduce (), reduce Right ()

Both iterate over all the values of the array and return a final value, the difference between them is the same as indexOf() and lastIndexOf().
Receive two parameters: a function invoked on each one and an initial value that serves as the basis for merging (optional)
The function passed contains four parameters, the former value, the current value, the index value of the item, and the array object.
For example:

//Find the sum of the terms of an array
var nums = [1,2,3,4,5];
var sum = nums.reduce(function(prev,curr,index,array)[
	return prev+curr;
});
console.log(sum);	


Welcome to discuss, progress!!!

Posted by henryblake1979 on Thu, 15 Aug 2019 03:50:58 -0700