Js array method

Array of common methods many, recently sorted out a little friends some reference

Reference Documents:

MDN-Array

ECMAScript6 portal – Array

Methods list

  • join()Β 
  • push()
  • pop()
  • shift()
  • unshift()
  • sort()
  • reverse()
  • concat()
  • slice()
  • splice()
  • IndexOf () (new in ES5)
  • LastIndexOf () (new in ES5)
  • ForEach () (new in ES5)
  • Map () (new in ES5)
  • Filter () (new in ES5)
  • Every () (ES5 added)
  • Some () (new in ES5)
  • Find () (new in ES6)
  • FindIndex () (new in ES6)
  • Includes () (new in ES6)

join()

Converts an array to a string, concatenated by the given character, by default,

Let foods = [' braised pork ', 'steamed fish ',' called chicken ']; console.log(foods.join()); / / output: "braise in soy sauce meat, steamed fish, call flower chicken". The console log (foods. Join (")); Console. log(food.join ('-')); // output: "console.log(food.join ('-')); // Output: "braised pork - steamed fish - Called chicken"Copy the code

push()

Adds one or more elements to the end of an array and returns the new array length

Let foods = [' chicken ', 'rabbit '] let length = foods.push(' duck ') console.log(length) // output :3 console.log(foods) // output: [' chicken ', 'rabbit', 'duck'] foods. Push (' geese ', 'the sparrow', 'swallow', 'cuckoo') to the console. The log (foods) / / output: [' chicken ', 'rabbit', 'duck', 'geese', 'the sparrow', 'swallow', 'cuckoo']Copy the code

pop()

Removes the last element from the array and returns the value of that element. Note: The length of the array changes

Let foods = [' ducky ',' pig '] let food = food.pop () console.log(food) // output: ducky console.log(foods) // output: [' ducky ']Copy the code

shift()

Removes the first element from the array and returns the value of that element. Note: The length of the array changes

Let amis = amis. Shift () console.log(ami)Copy the code

unshift()

Adds one or more elements to the beginning of an array and returns the new length of the array

Let foods = [' steamed fish ', 'braised fish '] let length = foods. Unshift (' twizzed-pork ') console.log(length) // output: 3 console.log(foods) // output: [' sichuan style stew pork "and" steamed fish ", "braise in soy sauce fish] foods. The unshift (' pot bag meat ', 'bacon'). The console log (foods) / / output: [' pot bag meat ', 'bacon'," sichuan style stew pork "and" steamed fish ", "braise in soy sauce fish]Copy the code

sort()

Sort the elements of an array and return an array. By default, array elements are compared to strings in UTF-16 order, and functions can also be passed in for sorting

Let numArr = [6, 2, 7, 1, 4, 9] console.log(numarr.sort ()) ,2,4,6,7,9 [1] let letterArr = [' e ', 'c' and 'z', 'd', 'f'] the console. The log (letterArr. Sort ()) / / output [' c ', 'd', 'e', 'f' and 'z'] let personArr = [{name: "jack", the age: 25}, {name: 'rose', age: 13 }, { name: 'lucy', age: 20}] console.log(// a and b are the elements compared in the array personarr.sort ((a, B) => {// return < 0 and equal to 0 a continues before b // return greater than 0 a and B will be transposed return a.age-b.agge})) // In ascending order by age, if you want to reverse order, can be transposedCopy the code

reverse()

Flips the array and returns the array. Note: What is changed is the original array

Let foods = [' braised fish ',' braised fish '] food.reverse () console.log(foods) // output: [' braised fish ',' braised fish ']Copy the code

concat()

Merges two or more arrays. Returns a new array, not changing the original array

Let foods = [' braised fish ', 'steamed fish '] let fruits = [' watermelon ',' banana '] console.log(foods.concat(fruits)) // output: [' braise in soy sauce fish ', 'steamed fish', 'watermelon', 'banana'] let meats = [' meat ', 'pork] the console. The log (foods. Concat (fruits, meats)) / / output: [' Braised fish ',' Steamed Fish ',' watermelon ',' banana ',' beef ',' pork ']Copy the code

slice()

Returns a new array object whose value is determined by the starting and ending indexes passed in

Let foods = [' braise in soy sauce fish ', 'steamed fish', 'watermelon', 'banana', 'beef', 'pork] the console. The log (foods) slice (5)) / / output: [' pork] the console. The log (foods. Slice (3, 5)) / / output: [' banana ', 'beef]Copy the code

splice()

Deleting a specified element based on the given start and end index changes the array

Let foods = [' braise in soy sauce fish ', 'cauliflower', 'watermelon', 'banana', 'beef', 'pork'] / / deleted from the index 1 1 element foods. The splice (1, 1). The console log (foods) / / output: [' braise in soy sauce fish ', 'watermelon', 'banana', 'beef', 'pork'] / / deleted from the index 2, remove all foods. The splice. (2) the console log (foods) output: / / / 'braise in soy sauce fish', 'watermelon'Copy the code

indexOf()

Returns the first qualified index value found in the array, or -1 if none exists

Let foods =[' watermelon ',' braised fish '] console.log(foods.indexof (' braised fish ')) // output: 1 console.log(foods.indexof (' steamed fish ')) // output: -1Copy the code

lastIndexOf()

The index is returned if the element is found, or -1 if it does not exist

Let foods =[' braised fish ',' watermelon ',' braised fish '] console.log(food.lastIndexof (' braised fish ')) 2 console.log(foods.lastIndexof (' steamed fish ')) // Output: -1Copy the code

forEach()

Pass each element of the array, one by one, to the callback that is passed in. The callback takes three arguments

  • element

    Current element.
  • index

    The index of the current element.
  • array

    callfindIndexThe array.
ForEach (v=>{console.log(v)}) let foods =[' braised fish ',' watermelon ',' steamed fish '] foods.foreach (v=>{console.log(v)}Copy the code

map()

Returns a new array, and the elements of the new array are the result of each element in the array being called to one of the provided functions. The callback function will take three arguments, respectively

  • element

    Current element.
  • index

    The index of the current element.
  • array

    callfindIndexThe array.
Let foods = [' braised fish ', 'watermelon '] let newFoods = food.map (v => {return v +' yummy '}) console.log(foods) // output: [' braise in soy sauce fish ', 'watermelon']. The console log (newFoods) / / output: [' braise in soy sauce fish delicious', 'watermelon delicious]Copy the code

filter()

Returns a new array in which each element of the array calls a provided function. The callback takes three arguments, one for each element, depending on whether the value returned is true or false

  • element

    Current element.
  • index

    The index of the current element.
  • array

    callfindIndexThe array.
Let foods = [' braise in soy sauce fish, "steamed fish", "sichuan style stew pork" and "pot bag meat] the console. The log (foods) filter (v = > {return v.i ndexOf (' fish ') = = 0})) / / output: [' Braised fish ', 'Steamed Fish ']Copy the code

every()

Verify that each value in the array satisfies the callback function’s checksum, which is true, or false, which takes three arguments, respectively

  • element

    Current element.
  • index

    The index of the current element.
  • array

    callfindIndexThe array.
Let numArr = [2, 5, 6, 7, 8, 9] console.log(numArr. Every (v => {return v > 2})) False console.log(numarr.every (v => {return v >= 2})) // Output: trueCopy the code

some()

Similar to every, the result of the callback is true if any element satisfies the check condition, and false if any element fails to satisfy the check condition, the callback will take three arguments

  • element

    Current element.
  • index

    The index of the current element.
  • array

    callfindIndexThe array.
Let numArr = [2, 5, 6, 7, 8, 9] console.log(numarr.some (v => {return v == 1})) False console.log(numarr.some (v => {return v == 2})) // Output: trueCopy the code

find()

Returns the value of the first element that satisfies the provided callback check. If neither element satisfies, the return undefined callback takes three arguments, respectively

  • element

    Current element.
  • index

    The index of the current element.
  • array

    callfindIndexThe array.
Find (v => {return v.indexof (' fo ')! Let foods = [' braised fish ', 'steamed fish ',' backcooked meat ', 'fo jump wall '] console.log(foods.find(v => {return v.indexof (' fo ')! Find (v => {return v.indexof (' watermelon ')! = -1}) // output: undefinedCopy the code

findIndex()

Returns the index of the first element in the array that satisfies the provided callback check, otherwise the callback that returns -1 takes three arguments, respectively

  • element

    Current element.
  • index

    The index of the current element.
  • array

    callfindIndexThe array.
Let foods = [{name: 'cabbage' color: "green"}, {name: 'pepper, color:' red '}, {name: 'carrots, color: 'white'}] console.log(foods.findIndex(v => {return v.color == 'white'})) 2 console.log(foods.findIndex(v => {return v.color == 'yellow'})) // Output: -1Copy the code

includes()

Returns whether the specified value exists in the array, true if so, false otherwise

Let foods = [' braise in soy sauce fish ', 'watermelon', 'steamed fish', 'pepper fish head]. The console log (foods. Includes (' watermelon')) / / output: True console.log(foods.includes(' twice-cooked pork ')) // output: falseCopy the code

At the end

These are usually more commonly used JS array methods, if there are some not mentioned welcome to add πŸ₯°πŸ˜˜ hope to help learn js array partners