An array of related operations - Method and Index

Recently a lot of learning methods and an array of array index, so its a finishing system

Array index 

// 索引:
var arr = ["hello",true,"world",2,5,"abc"];
console.log(arr) //["hello", true, "world", 2, 5, "abc"]
console.log(arr.length) // 6
// 增:
console.log(arr[0]) //hello
console.log(arr[arr.length-1]) //abc
console.log(arr[arr.length]) //undefined
arr[arr.length] = "hahahah";
console.log(arr[10]) //undefined
arr[10] = "heiheihie"
console.log(arr) //["hello",true,"world",2,5,"abc","hahahah",empty × 3,"heiheihie"]
console.log(arr[7]) //undefined
console.log(arr[8]) //undefined
console.log(arr[9]) //undefined
// 删:
console.log(arr.length) //11
arr.length = 4;
console.log(arr) //["hello", true, "world", 2]
// 改:
arr[1] = "qwe"
arr[3] = 123
console.log(arr) // ["hello", "qwe", "world", 123]
// // 查:
for(var i=0;i<arr.length;i++){
console.log(arr[i]) //hello
//qwe
//world
//123
}

 

An array of methods

1. increase the array elements

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

 
 
var arr = ["hello",true,123,"world"]

arr.push (456, "Hello"); the console.log (ARR);

 

Printed to the console display

 

1.2 unshift () to add one or more elements to the beginning of an array, and returns the new height

 
var arr = ["hello",true,123,"world"]

arr.push (456, "Hello");

arr.unshift(789); console.log(arr);

Printed to the console display

 

2. Delete the array elements

2.1 Removes and returns the last element of the array

 
 
var arr = ["hello",true,123,"world"]

arr.pop(); console.log(arr)

Printed to the console display

2.2 Delete and returns the first element of the array

 
 
var arr = ["hello",true,123,"world"]

arr.pop(); arr.shift(); console.log(arr);

Printed to the console display

 

3. Other methods array elements

3.1 Replace: arr.splice (m, n, data1, data2, ....), from the m start, a total of n, replaced data

 
var arr = ["hello",true,123,"world"]

arr.splice(1,2) ar
r.splice(1,2,"admin")
arr.splice(2,0,"admin")
arr.splice(2,0,"admin","a","b",345)
console.log(arr)

Printed to the console display

 

 3.2 interception: arr.slice (m, n); return selected elements from the existing array. The method takes two parameters slice (start, end), strat is required, starts from the first of several; end is optional, represents the first of several end (end position is not included), the last one are omitted; start and end can be thought negative, indicating the start counting from the last one, the last as -1 indicates a negative number.

 
 
var arr = ["hello",true,123,"world"]

 

var a = arr.slice(1,3)
  console.log(a)
  var a = arr.slice(1)
  console.log(a)

 

Printed to the console display

 

3.3 reverse () inverted array, return the results

var a=arr.reverse();
console.log(a)

Printed to the console display

3.4 sort () to sort the rules by character, returns the result

var a=arr.sort();
console.log(a)

Printed to the console display

3.5 valueOf () Returns the value of the original object array

 
 
var arr = ["hello",true,123,"world"]

arr.sort var a = ();
var a=arr.valueOf();
console.log(a)

Printed to the console display

3.6 toString () is converted into a string and returns;

 
 
var arr = ["hello",true,123,"world"]

var a=arr.toString(); console.log(a)

Printed to the console display

3.7 join () specified delimiter all the elements of the array into a string and returns the string.

join (str); optional parameters, defaults to "," number to the incoming character as a delimiter.

 
 
var arr = ["hello",true,123,"world"]

var a=arr.join("-"); console.log(a)

Printed to the console display

3.7 concat () is used to connect two or more arrays, the method does not change the existing array, but only returns a copy of the array is connected.

Create source array is omitted; concat (data1, data2, ...); all parameters Alternatively, data to be merged; data is an array, the combined data to the original array; data is added directly to the original data when the specific end of the array s copy.

 
var arr = ["hello",true,123,"world"]
var a = arr.concat ( "hello", "next"); 
the console.log (A)

Printed to the console display

3.8 indexOf () according to the specified data, from left to right, the position of the query appear in the array, if the specified data is not present, return -1. This method is a query method, it will not change the array.

var arr = ["hello",true,123,"world"]
 var a=arr.indexOf(123);
  console.log(a)

Printed to the console display

lastIndexOf () with indexOf () is from right to left.

 

3.9 forEach () ES5 new method, used to traverse the array, the method does not return a value. forEach received callback function in accordance with each execution of the array, the default callback function has three parameters, namely: traversing the data array corresponding to the index, the array itself.

 var arr = ["hello",true,123,"world"]
        var a = arr.forEach(function(value,index,self){
            
        console.log(value + "--" + index + "--" + (arr === self));

        })

        console.log(a)

Printed to the console display, console.log (a) is undefined because the method does not return value

3.10 map () 1. forEach same function; callback function will return the results 2.map, and finally map the return value of all the callback function to form a new array is returned.

var arr = ["hello",true,123,"world"]
var a = arr.map(function(value,index,self){

console.log(value + "--" + index + "--" + (arr === self));

})
var a = arr.map(function(value,index,self){

return "Hi"+value;

})

console.log(a)
console.log(arr)

Printed to the console display

 

3.11filter () 1. forEach same function; 2.filter callback function needs to return a Boolean value, if true, the number of times of the data is returned to this filter, and finally all the filter return value to form a new callback function returns an array ( this feature is understood as a "filter"). By default there are three parameters, namely value, index, self.

 

 

 var arr = ["hello",true,123,"world"]
        var a = arr.filter(function(value,index,self){

        console.log(value + "--" + index + "--" + (arr === self));

        })
        var a = arr.filter(function(value,index,self){

        return value.length>3;

        })

        console.log(a)
        console.log(arr)

Printed to the console display

3.12 every () to determine whether each item in the array conditions are met, only all the conditions are met, it will return true. every () receives as a parameter a callback, the callback function returns a value needed, every (callback); callback has three default parameters, respectively, value, index, self.

When the callback function returns true, the function similar forEach traversing all; if false, execution is stopped, the data is no longer traverse back, stopped at the first position returns false.

var arr = ["Tom","abc","Jack","Lucy","Lily","May"]; var a = arr.every(function(value,index,self){ console.log(value + "--" + index + "--" + (arr == self)) }) // 打印结果为: // Tom--0--true //因为回调函数中没有return true,默认返回undefined,等同于返回false //demo2: var arr = ["Tom","abc","Jack","Lucy","Lily","May"]; var a = arr.every(function(value,index,self){ console.log(value + "--" + index + "--" + (arr == self)) return value.length < 4; }) // 打印结果为: // Tom--0--true // abc--1--true // Jack--2--true //因为当遍历到Jack时,回调函数到return返回false,此时Jack已经遍历,但是后面数据就不再被遍历了 //demo3: var arr = ["Tom","abc","Jack","Lucy","Lily","May"]; var a = arr.every(function(value,index,self){ console.log(value + "--" + index + "--" + (arr == self)) return true; }) // 打印结果为: // Tom--0--true // abc--1--true // Jack--2--true // Lucy--3--true // Lily--4--true // May--5--true //因为每个回调函数的返回值都是true,那么会遍历数组所有数据,等同于forEach功能

 

When the return value of the callback function of each are true, every return is true, as long as there is a callback function returns a value of false, the return value of every false

var arr = ["Tom","abc","Jack","Lucy","Lily","May"];
    var a = arr.every(function(value,index,self){
        return value.length > 3;
    })
    console.log(a);           //false

    //demo2:
    var arr = ["Tom","abc","Jack","Lucy","Lily","May"];
    var a = arr.every(function(value,index,self){
        return value.length > 2;
    })
    console.log(a);           //true

Whether there is an entry satisfying the condition of 3.13 some () array is determined, as long as a condition is satisfied, returns true. some () receives as a parameter a callback, the callback function returns a value required, some (callback); callback has three default parameters, respectively, value, index, self.

        Because to judge each item in the array, as long as there is a callback function returns true, some will return true, and every so contrary, when the return value is true encounter a callback function, you can determine the result, then stop execution behind all data no longer traverse, the first stop in a position to return true; returns false when the callback function, you need to proceed backwards, to determine the final result, it will traverse all the data to achieve a similar forEach function, traverse all.

 

 var arr = ["Tom","abc","Jack","Lucy","Lily","May"];
    var a = arr.some(function(value,index,self){
        console.log(value + "--" + index + "--" + (arr == self))
        return value.length > 3;
    })
    // 打印结果为:
    // Tom--0--true
    // abc--1--true
    // Jack--2--true

    //demo2:
    var arr = ["Tom","abc","Jack","Lucy","Lily","May"];
    var a = arr.some(function(value,index,self){
        console.log(value + "--" + index + "--" + (arr == self))
        return true;
    var A = arr.some (function (value, index, Self) {
    // Demo3:
    // Tom - 0 - to true
    // print result:
    })

    var arr = ["Tom","abc","Jack","Lucy","Lily","May"];
        console.log(value + "--" + index + "--" + (arr == self))
        return false;
    })
    // 打印结果为:
    // Tom--0--true
    // abc--1--true
    // Jack--2--true
    // Lucy--3--true
    // Lily--4--true
    // May--5--true

 And every contrast, as long as there is a return value of the callback function are true, the return value is true some of all the callback function returns a value of false, only some of the return value is false

var arr = ["Tom","abc","Jack","Lucy","Lily","May"];
    var a = arr.some(function(value,index,self){
        return value.length > 3;
    })
    console.log(a);             //true

    //demo2:
    var arr = ["Tom","abc","Jack","Lucy","Lily","May"];
    var a = arr.some(function(value,index,self){
        return value.length > 4;
    })
    console.log(a);             //false

 

3.14 reduce () from the start of the first item in the array, traversed by one end, all iterations of the array, and then build a final value is returned. reduce () receives one or two parameters: the first one is the callback function calls represented in each of the array; second parameter (optional) as an initial value of the merge, the callback function is the first receiving a first parameter during execution.
reduce (callback, initial); callback default has four parameters, namely prev, now, index, self.
Any value will be returned as the callback parameter under the time of execution.
If the initial parameter is omitted, then the first iteration occurs on the second array, thus the first parameter is the callback to the first array, the second parameter is the second item in the array.

// demo1: no initial parameter is omitted, the callback function does not return a value 
    var ARR = [10,20,30,40,50]; 
    arr.reduce (function (PREV, now, index, Self) { 
        the console.log (PREV + "-" + now + "-" + index + "-" + (arr == Self)) 
    }, 2019) 
    // print result: 
    // 2019--10--0 - to true 
    // undefined - 20--1 - to true 
    // undefined - 30--2 - to true 
    // undefined - 40--3 - to true 
    // undefined - 50--4 - to true 
    // this time callback function does not return, it starts from the second, to get the PREV is undefined 

    // demo2: Initial parameter is omitted, the callback function does not return a value 
    ARR = var [10,20,30,40,50]; 
    arr.reduce ( function (PREV, now, index, Self) { 
        the console.log (PREV + "-" now + + "-" + index + "-" + (ARR == Self)) 
    })
    // print the results as follows: first, the first argument to the callback function is the first item in the array. The second parameter is an array of second 
    // 10--20--1 - to true 
    // undefined - 30--2 - to true 
    // undefined - 40--3 - to true 
    // undefined- -50--4 - to true 
    // in this case no return callback function, it starts from the second, to get the PREV is undefined 

    // Demo3: initial parameter is not omitted, the callback function returning 
    var arr = [10, 20,30,40,50]; 
    arr.reduce (function (PREV, now, index, Self) { 
        the console.log (PREV + "-" now + + "-" + index + "-" + ( Self == ARR)); 
        return "Hello"; 
    }, 2019) 
    // print result: 
    // 2019--10--0 - to true 
    // Hello - 20--1 - to true 
    // Hello- -30--2 - to true 
    // the Hello - 40--3 - to true  
    // the Hello - 50--4 - to true
    // callback function at this time there is return, so starting from the second, prev got the return value of the callback function

    // demo4: initial parameter is omitted, the callback function returning 
    var ARR = [10,20,30,40,50]; 
    arr.reduce (function (PREV, now, index, Self) { 
        the console.log (PREV + " - "now + +" - "+ index +" - "+ (ARR == Self)); 
        return" Hello "; 
    }) 
    // Print results: first, the first argument to the callback function It is the first item in the array. The second parameter is an array of second 
    // 10--20--1 - to true 
    // the Hello - 30--2 - to true 
    // the Hello - 40--3 - to true 
    // Hello- -50--4 - to true 
    // At this point there is a callback function return, so that starting from the second, prev is the value of the callback function to get the return 

    // demo5: reduce calculation using an array of all the data and 
    var arr = [10,20,30,40,50]; 
    var arr.reduce = SUM (function (PREV, now, index, Self) { 
        return now PREV +; 
    the console.log (SUM); // 150
    // return the result of the last callback function is returned to the body of the reduce method 

    // demo6: reduce calculation using an array of all the data and 
    var ARR = [10,20,30,40,50]; 
    var = SUM ARR. reduce (function (PREV, now, index, Self) { 
        return now PREV +; 
    },. 8) 
    the console.log (SUM); 158 // 
    // last callbacks return a result of the method is returned to reduce body 
    / / second parameter as reduce initial, is calculated in the first execution, the final result is added 8

 

 

 

 

 

Guess you like

Origin www.cnblogs.com/mhcll/p/11417936.html