Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the built-in methods of javascript array

2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces what are the built-in methods of javascript array, the article is very detailed, has a certain reference value, interested friends must read it!

1.Array.at ()

Function: accepts an integer value and returns the item at the index, allowing positive and negative integers. Negative integers are counted from the last item in the array.

Syntax: Array.at (index)

Parameter: index: the index (position) of the array elements to be returned. When passing a negative index, relative indexing from the end of the array is supported; that is, if a negative number is used, the returned element is found by counting backwards from the end of the array.

Return value: the element in the array that matches the given index. Undefined returns if the given index cannot be found

Var arr = [1 arr.at (- 1); console.log (newarr); / / 5 var newarr = arr.at (3); console.log (newarr); / / 42. Array.copyWithin ()

Function: shallow copy part of the array to another location in the same array and return it without changing the length of the original array.

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

Parameters:

Target:

0 is the base index, and copy the sequence to that location. If it is negative, the target will be calculated from the end.

If target is greater than or equal to arr.length, no copy will occur. If target is after start, the copied sequence will be modified to conform to arr.length.

Start:

0 is the base index and starts to copy the starting position of the element. If it is negative, the start will be calculated from the end.

If start is ignored, copyWithin will be copied starting at 0.

End:

0 as the base index, start copying the end position of the element. CopyWithin will be copied to that location, but elements in the end location will not be included. If it is negative, the end will be calculated from the end.

If end is ignored, the copyWithin method will be copied all the way to the end of the array (default is arr.length)

Var arr = [1 console.log 2, 3 console.log 5]; var arr2= arr.copyWithin (- 2) console.log (arr2); / / [1 console.log (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] 3. Array.entries ()

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

Syntax: arr.entries ()

Return value: a new Array iterator object. Array Iterator is an object, and its prototype (_ _ proto__:Array Iterator) has a next method that can be used to iterate through the [key,value] of the original array.

Var array1 = ['a', 'baked,' 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"] 4. Array.fill ()

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

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

Parameters:

Value: the value used to populate the array elements.

Start: optional, starting index, default value is 0.

End: optional, terminates the index. The default value is this.length.

Return value: modified array

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] 5.find ()

Function: returns the value of the first element in the array that satisfies the provided test function. Otherwise, undefined is returned.

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

Parameters:

Callback: a function executed on each item of the array that takes three arguments:

Element: the element currently traversed.

Index: optional, currently traversed index.

Array: optional, the array itself.

ThisArg is optional and is used as an object for this when performing a callback.

Return value: the value of the first element in the array that satisfies the provided test function, otherwise undefined is returned.

Var array1 = [5,12,8,130,44]; var found = array1.find (element = > element > 10); console.log (found); / / 12 is all the content of the article "what are the built-in methods of the javascript array"? thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report