javascript
数组的fill方法_数组fill()方法以及JavaScript中的示例
數組的fill方法
JavaScript fill()方法 (JavaScript fill() method)
fill() method is used fill the array with a given value.
fill()方法用于使用給定值填充數組。
Syntax:
句法:
array.fill(value, [start_index], [end_index]);Parameters:
參數:
value is a static value to be filled in the array.
value是要填充到數組中的靜態值。
start_index is an optional parameter, it specifies the starting index to fill the array, its default value is 0.
start_index是一個可選參數,它指定填充數組的起始索引,其默認值為0。
end_index is also an optional parameter, it specifies the ending index to fill the array, its default value is array.length.
end_index也是一個可選參數,它指定填充數組的結束索引,其默認值為array.length 。
Return value: None
返回值:無
Example:
例:
Input:var arr = [10, 20, 30, 40, 50];Function call:arr.fill(0);Output:0,0,0,0,0JavaScript Code to demonstrate example of Array.fill() method
JavaScript代碼演示Array.fill()方法的示例
<html> <head> <title>JavaScipt Example</title> </head><body><script>var arr = [10, 20, 30, 40, 50];document.write("array elements: " + arr + "<br>");//filling all elementsarr.fill(0);document.write("array elements: " + arr + "<br>");//filling initial 2 elements with 100arr.fill(10, 0, 2);document.write("array elements: " + arr + "<br>");//filling next 3 elements with 200arr.fill(200, 2, 5);document.write("array elements: " + arr + "<br>"); </script> </body> </html>Output
輸出量
array elements: 10,20,30,40,50 array elements: 0,0,0,0,0 array elements: 10,10,0,0,0 array elements: 10,10,200,200,200翻譯自: https://www.includehelp.com/code-snippets/array-fill-method-with-example-in-javascript.aspx
數組的fill方法
總結
以上是生活随笔為你收集整理的数组的fill方法_数组fill()方法以及JavaScript中的示例的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 7种可能会导致内存泄漏的场景!
- 下一篇: 图文并茂的聊聊Java内存模型!