The splice() method changes the contents of an array by removing or replacing existing elements and/or adding new elements in place.

array.splice(start[, deleteCount[, item1[, item2[, ...]]]]

会修改原数组!

  1. var months = ['Jan', 'March', 'April', 'June'];
  2. months.splice(1, 0, 'Feb');
  3. // inserts at 1st index position
  4. console.log(months);
  5. // expected output: Array ['Jan', 'Feb', 'March', 'April', 'June']
  6. months.splice(4, 1, 'May');
  7. // replaces 1 element at 4th index
  8. console.log(months);
  9. // expected output: Array ['Jan', 'Feb', 'March', 'April', 'May']

The slice() method returns a shallow copy of a portion of an array into a new array object selected from begin to end (end not included). The original array will not be modified.


arr.slice([begin[, end]

不会修改原数组!
_

  1. var animals = ['ant', 'bison', 'camel', 'duck', 'elephant'];
  2. console.log(animals.slice(2));
  3. // expected output: Array ["camel", "duck", "elephant"]
  4. console.log(animals.slice(2, 4));
  5. // expected output: Array ["camel", "duck"]
  6. console.log(animals.slice(1, 5));
  7. // expected output: Array ["bison", "camel", "duck", "elephant"]

The find() method returns the value of the first element in the array that satisfies the provided testing function. Otherwise undefined is returned.

The find method executes the callback function once for each index of the array until it finds one where callback returns a true value.

arr.find(callback(element[, index[, array]])[, thisArg])

  1. var array1 = [5, 12, 8, 130, 44];
  2. var found = array1.find(function(element) {
  3. return element > 10;
  4. });
  5. console.log(found);
  6. // expected output: 12

The findIndex() method returns the index of the first element in the array that satisfies the provided testing function. Otherwise, it returns -1, indicating no element passed the test.

arr.findIndex(callback(element[, index[, array]])[, thisArg])

  1. var array1 = [5, 12, 8, 130, 44];
  2. function isLargeNumber(element) {
  3. return element > 13;
  4. }
  5. console.log(array1.findIndex(isLargeNumber));
  6. // expected output: 3

image.png
image.png


image.png
image.png


Find the middle point

image.png

Circular list?

if (slow === fast) return true;
image.png

Step back from tail

image.png