Javascritp Array Array Methods

The combined arrays - concat ()

Use a (merge two arrays)


var hege = ["Cecilie", "Lone"];

var stale = ["Emil", "Tobias", "Linus"];

var children = hege.concat(stale);

console.log(children );      // ["Cecilie", "Lone", "Emil", "Tobias", "Linus"]

Usage of Two (Merge multiple arrays)


var parents = ["Jani", "Tove"];

var brothers = ["Stale", "Kai Jim", "Borge"];

var children = ["Cecilie", "Lone"];

var family = parents.concat(brothers, children);

console.log(family);          // ["Jani", "Tove", "Stale", "Kai Jim", "Borge", "Cecilie", "Lone"]

Array to a string - join (), toString ()

Both return new string

join () method


var fruits = ["Banana", "Orange", "Apple", "Mango"];

var a = fruits.join();

console.log(a);                // "Banana,Orange,Apple,Mango"

toString () method


var fruits = ["Banana", "Orange", "Apple", "Mango"];

var a = fruits.toString();

console.log(a);                // "Banana,Orange,Apple,Mango"

Same point:

All can turn a string array.

the difference:

There is another join Usage: join () accepts a parameter elements as the connection between the symbol


var fruits = ["Banana", "Orange", "Apple", "Mango"];

var a = fruits.join("-");

console.log(a);            //  "Banana-Orange-Apple-Mango"

To delete the last element of the array - pop ()

Returns the last element of pop elements, the last delete the original array.


var fruits = ["Banana", "Orange", "Apple", "Mango"];

var a = fruits.pop();

console.log(a);                //  "Mango"

console.log(fruits );          //  ["Banana", "Orange", "Apple"]

Adding elements - push ()

push returns the length of the array, add a new element source array rearmost


var fruits = ["Banana", "Orange", "Apple", "Mango"];

var a = fruits.push("Kiwi");

console.log(a);                // 5

console.log(fruits);          //   ["Banana", "Orange", "Apple", "Mango", "Kiwi"]

Array reverse - reverse ()


var fruits = ["Banana", "Orange", "Apple", "Mango"];

fruits.reverse();

console.log(fruits);        // ["Mango", "Apple", "Orange", "Banana"]

The first element of the array delete - shift ()

shift returns the first element of the array, the first element of the original array is deleted


var fruits = ["Banana", "Orange", "Apple", "Mango"];

var a= fruits.shift();

console.log(a);              //  "Banana"

consoel.log(fruits);        //   ["Orange", "Apple", "Mango"]

Taken array element - slice (start, end)

slice taken Returns an array of elements, it does not change the original array.

slice accepts two parameters, a start position and an end position, the starting position if the first argument is negative, indicating the start counting from the end of the array, such as a -1 is the last. If you do not pass the end position, it represents the end of an array of all the elements of play from the start position.


var fruits = ["Banana", "Orange", "Lemon", "Apple", "Mango"];

var a= fruits.slice(1,3);

console.log(a);                //  ["Orange", "Lemon"]

console.log(fruits);          //   ["Banana", "Orange", "Lemon", "Apple", "Mango"]

Array Sort - sort (func)

The sort method for sorting elements of the array.

sort parameters a pharmaceutically func, non-essential pass, if the function must be passed, without parameter passing, sort order to sort string represents the encoding, if required to sort the array passed to the function.

Does not pass parameters:


var  arr= ["George", "John", "Thomas", "James", "Adrew", "Martin"];

arr.sort();

console.log(fruits);          //  ["Adrew", "George", "James", "John", "Martin", "Thomas"]

// 没有传入函数,所以结果是按照0123456789的顺序进行排列

var nums = [21,14,26,11,105];

nums.sort();

console.log(nums);            //  [105, 11, 14, 21, 26]

Parameter passing:


function sortNumber(a, b){

        return a - b

}

var nums = [21,14,26,11,105];

nums.sort(sortNumber);

console.log(nums);            // [11, 14, 21, 26, 105]

Add in the specified location / delete / replace elements - splice (start, count, .....)

receiving a plurality of parameters splice, is a first start position, the second number is deleted, then the parameters for the add / delete / replacement element.

splice returned delete elements in the array

Use a (add elements)


var fruits = ["Banana", "Orange", "Apple", "Mango"];

fruits.splice(2,0,"Lemon","Kiwi");

console.log(fruits );            //  ["Banana", "Orange", "Lemon", "Kiwi", "Apple", "Mango"]

Usage of Two (replacement element)


// 从第二个位置起删除两个元素,接受新的两个元素

var fruits = ["Banana", "Orange", "Apple", "Mango"];

var a = fruits.splice(2,2,"Lemon","Kiwi");

console.log(a);                  // ["Apple", "Mango"]

console.log(fruits);            //  ["Banana", "Orange", "Lemon", "Kiwi"]

Use three (delete elements)


var fruits = ["Banana", "Orange", "Apple", "Mango"];

var a = fruits.splice(2,2);

console.log(a);                  // ["Apple", "Mango"]

console.log(fruits);            //  ["Banana", "Orange"]

Add an element at the beginning of the array - unshift ()


var fruits = ["Banana", "Orange", "Apple", "Mango"];

fruits.unshift("Lemon","Pineapple");

console.log(fruits);            // ["Lemon", "Pineapple", "Banana", "Orange", "Apple", "Mango"]

Check whether there is an element in the array - indexOf (), lastIndexOf ()

indexOf and lastIndexOf accept two parameters, the first element is to be checked. The second is to start checking from that index position

indexOf represents from 0, lastIndexOf represents the beginning of the last element


var  arr = [1,23,4,5,6,7,89];

arr.indexOf(89)                //    6

arr.indexOf(89,2)              //    6

arr.lastIndexOf(89)            //    6

arr.lastIndexOf(89,5)          //    -1

Array traversal - map (), forEach ()

Same point:

  • The method is traversing the array, each element in the array can traverse the

  • forEach and map each execution method in an anonymous function supports three parameters, the parameters are the item (currently each), index (index value), arr (original array)

  • We will not change the original array

  • this anonymous function both point to windows

difference:

  • map method returns a new array, the array element value after processing, forEach method does not return a new array

  • map method will not detect an empty array, forEach for an empty array will not call the callback function.

  • mao method does not support low-end browsers


var arr = [1,2,3,4];

var a = arr.map(function(item,index,arr){

    return item * item;

})

console.log(a);        //  [1, 4, 9, 16]

console.log(arr);      //   [1, 2, 3, 4]

var arr = [1,2,3,4];

var a = arr.forEach(function(item,index,arr){

    return item * item;

})

console.log(a);        //  undefined

console.log(arr);      //   [1, 2, 3, 4]

Array filter - filter ()

the filter () return of an array of elements that satisfies a condition, the method returns a new array.

// returns the element is greater than the length of the array elements 10


var arr =  [12,5,8,16,125,98];

var filters = arr.filter(function(value){

      return value >= 10

});

console.log(filters );        //  [12, 16, 125, 98]

of traversal


let arr2=[1,2,234,'sdf',-2];

for(let a of arr2){

  console.log(a)          // 1,2,234,sdf,-2 遍历了数组arr的值

}

Array accumulator - reduce (), reduceRight ()

reduce () method takes a function as an accumulator, each value in the array (left to right) started to shrink, as a final calculated value.

reduce usage and reduceRight same, the difference is that reduceRight is done from the end of the array is an array of items accumulated forward.


array.reduce(function(total, currentValue, currentIndex, arr), initialValue);

total         必需。初始值, 或者计算结束后的返回值。

currentValue    必需。当前元素

currentIndex    可选。当前元素的索引

arr         可选。当前元素所属的数组对象。

initialValue    初始值

var numbers = [65, 44, 12, 4];

var a = numbers.reduce(function(total,num){

      return total + num;

},0)

console.log(a);          // 125

Analyzing element in the array satisfies the specified conditions - some (), every ()

some methods go through each element of the array, it determines whether the condition, returns a Boolean value.

every method iterates through each element of the array to determine whether each condition is satisfied, if each are met, it returns true.


var numbers = [65, 44, 12, 4];

var a = numbers.some(function(item){

      return item > 62;

});

console.log(a);          //  true

var numbers = [65, 44, 12, 4];

var a = numbers.every(function(item){

      return item > 62;

});

console.log(a);          //  false

Copy the current array element designated location to another location and replacement - copyWithin (index, start, end)

index (required): Replace data starting at that position. If negative, it represents the countdown.

start (optional): starts to read data from that location, the default is 0. If negative, it represents the countdown.

end (optional): to read data before the stop position, by default equal to the array length. Use may be negative at a predetermined position from the end of the array.


[1, 2, 3, 4, 5].copyWithin(0,1,3)    //   [2, 3, 3, 4, 5]

JSON array format conversion - Array.from ()

Array.from () json required length must be converted properties


let  people = {

    0:'zhangsan',

    '1':24,   

    length:2   

};

let trans=Array.from(people);

console.log(trans);            //  ['zhangsan',24]

Converting the pile element into an array - Array.of ()


let arr = Array.of(1,"23","测试","dsa"); 

console.log(arr);            //  [1, "23", "测试", "dsa"]

Find the array to meet the conditions of the elements - find ()


let arr=[1,2,3,"cxz",-2];

var a = arr.find(function(x){

  return x<="cxz";

});

console.log(a);              // "cxz"

Find elements in the array to meet the conditions of the index - findIndex ()


let arr=[1,2,3,"cxz",-2];

var a = arr.findIndex(function(x){

  return x<="cxz";

});

console.log(a);              // 3

Determining if a certain element in the array - includes ()

includes determining whether to include an element array, returns a Boolean value


let arr=[1,2,3,"cxz",-2];

var a = arr.includes("cxz");

console.log(a);              //  true

Finally, we welcome the attention to my personal public number, Internet code of agriculture, focus on Internet programming technology sharing, public concern number, return keywords, you can receive a series of programmed learning videos Oh, the front end of, java, ios, Andrews, c ++, python applications do have .

Guess you like

Origin www.cnblogs.com/blogcxz/p/11100033.html