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

Recommend

One sql statement completes MySQL deduplication and keeps one

A few days ago, when I was working on a requireme...

How to install Graphviz and get started tutorial under Windows

Download and installConfigure environment variabl...

Detailed application of Vue dynamic form

Overview There are many form requirements in the ...

How to handle token expiration in WeChat Mini Programs

Table of contents Conclusion first question Solut...

Detailed explanation of nginx-naxsi whitelist rules

Whitelist rule syntax: BasicRule wl:ID [negative]...

Detailed graphic explanation of how to use svg in vue3+vite project

Today, in the practice of vue3+vite project, when...

Use html-webpack-plugin' to generate HTML page plugin in memory

When we package the webpackjs file, we introduce ...

Security considerations for Windows server management

Web Server 1. The web server turns off unnecessar...

Issues with using Azure Container Registry to store images

Azure Container Registry is a managed, dedicated ...

Installation and configuration of MySQL 5.7.17 free installation version

MYSQL version: MySQL Community Server 5.7.17, ins...

How to start and restart nginx in Linux

Nginx (engine x) is a high-performance HTTP and r...

A brief discussion on logic extraction and field display of Vue3 in projects

Table of contents Logical Layering Separate busin...

Control the vertical center of the text in the HTML text box through CSS

When the height attribute of Text is defined, the ...