In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article will explain in detail how to create an array in JavaScript. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.
JavaScript creates an array
1. Using new to create arrays
Var arr = new Array (); / / created an empty array
two。 Use the literals of arrays to create arrays (more commonly used)
Literal quantity: a representation of a fixed value
You can know what data type at a glance, for example, 8 is a numeric type at a glance.
/ / 1. Use array literals (square brackets) to create an array var arr = []; / / create an empty array var arr1 = [1]; / / any type of data can be stored in the array and must be separated by commas.
3. What are array elements?
An array can store a set of data under a collection of individual variables. Array elements are elements stored in an array with no restrictions on their types.
The use of arrays
1. Get array elements-using the index is the subscript, which starts at 0, much like the c language
Console.log (arr [2])
two。 Ergodic array
Var arr = ['red','green','blue']; for (var I = 0 [I]
< 3;i++){ console.log(arr[i]);}//打印数组长度console.log(arr.length);//动态检测数组长度for(var i = 0;i < arr.length;i++){ console.log(arr[i]); } 什么是遍历? 将数组元素从头到尾访问一次,里面的i是计数器当索引号使用,arr[i]访问数组元素,索引号和数组长度没有关系。 //经典案例打印数组最大值var arr = [2,6,1,77,52,25,7,99];var max = arr[0];for(var i = 0;i < arr.length;i++){ if(max < arr[i]){ max = arr[i]; } } conaole.log('该数组里面的最大值是' + max);将数组转换为分割字符串var arr = ['red','green','blue','pink'];var str ='';for(var i = 0;i < arr.length;i++){ str+=arr[i]+'|';//也可以var sep = '!';str+=arr[i]+sep;}console.log(str);数组中新增元素var arr = ['red','green','blue','pink'];var str ='';for(var i = 0;i < arr.length;i++){ str+=arr[i]+'|';//也可以var sep = '!';str+=arr[i]+sep;}console.log(str); 如果索引号存在就是替换数组元素,不要直接给数组名赋值,否则会覆盖掉以前的数据例如 arr1 ='';onsole.log(arr1) 筛选数组var arr = [1,2,3,4,5,6,7,8,9,10];var newArr = [];console.log(newArr);for(var i = 0;i < arr.length;i++){ if(arr[i]>= 5) {newArr [newArr.length] = arr [I];}} Delete the specified element of the array / / delete 7 var arr = [1Ling 2 Ling 3 Jing 4 Jing 5 Jing 6 Jing 8 Ji 9 Jing 10]; var newArr = []; console.log (newArr); for (var I = 0 arr.length;i++ I < arr.length;i++) {if (Arri]! = 7) {newArr [newArr.length] = arr [I];}} console.log (newArr); this is the end of the article on "how to create an array in JavaScript". I hope the above content can be helpful to you so that you can learn more knowledge. if you think the article is good, please share it for more people to see.
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.