Detailed explanation of built-in methods of javascript array

Detailed explanation of built-in methods of javascript array

1. Array.at()

Function : Accepts an integer value and returns the item at that index, both positive and negative integers are allowed. Negative integers count from the last item in the array.

Syntax : Array.at(index)

Parameters : index: The index (position) of the array element to be returned. Relative indexing from the end of the array is supported when a negative index is passed; that is, if a negative number is used, the returned element will be found by counting backwards from the end of the array.

Return value : The element in the array that matches the given index. Returns undefined if the given index is not found

<script type="text/javascript">
			var arr = [1,2,3,4,5];
			var newarr = arr.at(-1);
			console.log(newarr); // 5
			var newarr = arr.at(3);
			console.log(newarr); // 4
</script>

2. Array.copyWithin()

Function : Shallowly copy part of an array to another position in the same array and return it without changing the length of the original array.

Syntax : arr.copyWithin(target[, start[, end]])

parameter:

target:

0 is the index of the basis, and the sequence is copied to this position. If it is a negative number, target will be counted from the end.

If target is greater than or equal to arr.length, no copying will occur. If target follows start, the copied sequence will be modified to fit arr.length.

start:

0 is the base index, the starting position from which to start copying elements. If negative, start will count from the end.

If start is omitted, copyWithin will start copying from 0.

end:

0 is the base index, starting at the end position of the copied elements. copyWithin will copy to that position, but not including the element at end. If negative, end will count from the end.

If end is omitted, the copyWithin method will copy to the end of the array (default is arr.length)

<script type="text/javascript">
			var arr = [1,2,3,4,5];
			var arr2 = arr.copyWithin(-2)
			console.log(arr2); // [1, 2, 3, 1, 2]
			var arr3 = arr.copyWithin(0, 3)
			console.log(arr3); // [4, 5, 3, 4, 5]
			var arr4 = arr.copyWithin(0, 3, 4)
			console.log(arr4); // [4, 2, 3, 4, 5]
			var arr5 = arr.copyWithin(-2, -3, -1)
			console.log(arr5); // [1, 2, 3, 3, 4]
</script>

3. Array.entries()

Function : Returns a new Array Iterator object that contains the key/value pairs for each index in the array.

Syntax : arr.entries()

Return value : A new Array iterator object. Array Iterator is an object. Its prototype (__proto__:Array Iterator) has a next method that can be used to traverse the iterator to obtain the [key, value] of the original array.

<script type="text/javascript">
			var array1 = ['a', 'b', 'c'];
			var iterator1 = array1.entries();
			console.log(iterator1.next().value);
			// expected output: Array [0, "a"]
			console.log(iterator1.next().value);
			// expected output: Array [1, "b"]
</script>

4. Array.fill()

Function : Fill all elements in an array from the start index to the end index with a fixed value. The ending index is not included.

Syntax : arr.fill(target[, start[, end]])

parameter :

  • value: The value used to fill the array elements.
  • start : Optional, starting index, default value is 0.
  • end : optional, ending index, default value is this.length.

Return value : the modified array

<script type="text/javascript">
			var array1 = [1, 2, 3, 4];	
			// fill with 0 from position 2 until position 4
			console.log(array1.fill(0, 2, 4));
			// expected output: [1, 2, 0, 0]
			// fill with 5 from position 1
			console.log(array1.fill(5, 1));
			// expected output: [1, 5, 5, 5]
			console.log(array1.fill(6));
			// expected output: [6, 6, 6, 6]
</script>

5. find()

Function: Returns the value of the first element in an array that satisfies the provided test function. Otherwise returns undefined.

Syntax: arr.find(callback[, thisArg])

parameter:

  • callback: A function to be executed on each item in the array, receiving 3 parameters:
  • element: The element currently traversed.
  • index: optional, the current index traversed.
  • array: optional, the array itself.
  • thisArg is optional. The object used as this when executing the callback.

Return value: The value of the first element in the array that satisfies the provided test function, otherwise it returns undefined.

<script type="text/javascript">
			var array1 = [5, 12, 8, 130, 44];
			var found = array1.find(element => element > 10);
			console.log(found); // 12
</script>

Summarize

This article ends here. I hope it can be helpful to you. I also hope that you can pay more attention to more content on 123WORDPRESS.COM!

You may also be interested in:
  • An article to help you learn more about JavaScript arrays
  • Detailed explanation of several methods of deduplication in Javascript array
  • Commonly used JavaScript array methods
  • JavaScript Array Detailed Summary
  • JavaScript commonly used array deduplication actual combat source code
  • How to monitor array changes in JavaScript
  • Examples and comparison of 3 methods for deduplication of JS object arrays
  • JS implements array filtering from simple to multi-condition filtering
  • JavaScript array reduce() method syntax and example analysis
  • Implement 24+ array methods in JavaScript by hand

<<:  MySQL takes out the comma-separated values ​​from a field to form a new field

>>:  Detailed tutorial of pycharm and ssh remote access server docker

Blog    

Recommend

How to load the camera in HTML

Effect diagram: Overall effect: Video loading: Ph...

Understanding of haslaylout and bfc parsing

1. haslayout and bfc are IE-specific and standard ...

Modularity in Node.js, npm package manager explained

Table of contents The basic concept of modularity...

Detailed explanation of how to upgrade software package versions under Linux

In the Linux environment, you want to check wheth...

How to view the running time of MySQL statements through Query Profiler

The previous article introduced two methods to ch...

How to add color mask to background image in CSS3

Some time ago, during development, I encountered ...

JavaScript object-oriented class inheritance case explanation

1. Object-oriented class inheritance In the above...

Common failures and reasons for mysql connection failure

=================================================...

Example code of javascript select all/unselect all operation in html

Copy code The code is as follows: <html> &l...

Solution for multiple Docker containers not having the same port number

Background In Docker, four containers are created...

Tutorial on upgrading from Centos7 to Centos8 (with pictures and text)

If you upgrade in a formal environment, please ba...

Detailed example of deploying Nginx+Apache dynamic and static separation

Introduction to Nginx dynamic and static separati...

How to configure wordpress with nginx

Before, I had built WordPress myself, but at that...

Detailed explanation of three ways to import CSS files

There are three ways to introduce CSS: inline sty...