Home  >  Article  >  Web Front-end  >  Which methods in js will change the original array

Which methods in js will change the original array

下次还敢
下次还敢Original
2024-05-10 04:51:191277browse

Methods that will change the original array: push(): add elements to the end of the array pop(): remove elements at the end of the array shift(): remove elements at the beginning of the array unshift(): add at the beginning of the array Element splice(): Remove/replace/insert elements sort(): Sort elements reverse(): Reverse the order of elements fill(): Fill the array with values ​​

Which methods in js will change the original array

Which methods in JS will change the original array

There are many methods in JS that can be used to operate arrays, some of which will modify the original array, while other methods will return a new array.

Methods that will change the original array:

  • push(): Add an element to the end of the array.
  • pop(): Remove the element at the end of the array.
  • shift(): Remove the element at the beginning of the array.
  • unshift(): Add an element to the beginning of the array.
  • splice(): Removes or replaces elements from the array, or inserts new elements at the specified position.
  • sort(): Sort the elements in the array.
  • reverse(): Reverse the order of elements in the array.
  • fill(): Fills the elements in the array with the given value.

Methods that do not change the original array:

  • slice(): Returns a new copy of the array. The copy starts from the specified position and ends at the specified position.
  • concat(): Returns a new array that is composed of the original array and other provided parameters.
  • map(): Creates a new array that is converted from each element of the original array through the given function.
  • filter(): Creates a new array consisting of elements in the original array that meet the given conditions.
  • reduce(): Iterate over the elements in the array one by one and reduce them to a single value.

The above is the detailed content of Which methods in js will change the original array. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:Usage of class in jsNext article:Usage of class in js