Microsoft®
JScript Array Object |
Language Reference Version 2
|
Provides support for creation of arrays of any data type.
new Array()
new Array(size)
new Array(element0, element1, ..., elementn)[element0, element1, ..., elementn]
The Array object creation syntax has these parts:
Part Description size The size of the array. As arrays are zero-based, created elements will have indices from zero to size - 1. element0,...,elementn The elements to place in the array. This creates an array with n + 1 elements, and a length of n.
After an array is created, the individual elements of the array can be accessed using [ ] notation, for example, x = my_array[4];.Since arrays in Javascript are zero-based, the preceding example accesses the fifth element of the array. This is a common source of confusion for new programmers. The first element of an array is element zero. e.g. my_array[0] is the element 1. Also: let last = my_array[my_array.length-1];
In some languages, arrays are implemented as simple allocations of memory, one element after the other. In Javascript, arrays are simply objects, with numerical attributes. In other words, the elements are simply variables named "1", "2", "3", etc... inside the array object. So you can, e.g.
let my_array = new Array(6);
my_array[1000]="hello";
In C, for example, that would cause an error at best, and at worse, crash the program.Every science fiction fan knows that to crash a computer you present it with a fiendish paradox.
Every programmer knows that "here are six boxes, please put something in box number six" is sufficient.
-- Gordon CharltonSince Javascript arrays are just objects, they can have named attributes. e.g. my_array["item"] = 1; will work, but only the numeric attributes will be indexed with methods like join, slice, or sort (although it will retain them), but non-numeric attributes will be included with iterating methods like, or for...in.
If only one argument is passed to the Array constructor, and it is a number, it is coerced into an unsigned integer and the value is used as the initial size of the array. Otherwise, the parameter passed in is used as the only element of the array. If multiple arguments are passed in, they become the elements of the array. The short form version of this is to simply use square brackets. e.g.
let my_array = [1, 2, 3, "hello"]; console.log(my_array[0]); //Will print "1" console.log(my_array[3]); //Will print "hello" console.log(typeof(my_array)); //Shows "Object" a = [0, 1, 2]; a.push(3); //returns the length 4, a is [0, 1, 2, 3] a.pop(); //returns the last element 3, a is [0, 1, 2] a.shift(); //returns the first element 0, a is [1, 2] a.unshift(0); //returns the length 3, a is [0, 1, 2] ['a', 'b'].concat('c'); //['a','b','c'] ['a', 'b', 'c'].join('-'); // "a-b-c" ['a', 'b', 'c'].slice(1); // ['b', 'c'] ['a', 'b', 'c'].indexOf('b'); // 1 ['a', 'b', 'c'].includes('c'); // true [3, 5, 6, 8].find((n) => n % 2 === 0) //6 [2, 4, 3, 5].findIndex((n) => n % 2 !== 0) //2 [3, 4, 8, 6].map((n) => n * 2) // [6,8,16,12] [1, 4, 7, 8].filter((n) => n % 2 === 0) // [4,8] [2, 4, 3, 7].reduce((acc, cur) => acc+cur) //16 [2, 3, 4, 5].every((x) => x < 6) //true [3, 5, 6, 8].some((n) => n > 6) //true [1, 2, 3, 4].reverse() // [4,3,2,1] [3, 5, 7, 8].at(-2) //7
© 1997 by Microsoft Corporation. All rights reserved.
See also:
file: /Techref/inet/iis/jscript/htm/js528.htm, 6KB, , updated: 2022/3/1 21:29, local time: 2024/11/22 06:04,
18.223.237.218:LOG IN
|
©2024 These pages are served without commercial sponsorship. (No popup ads, etc...).Bandwidth abuse increases hosting cost forcing sponsorship or shutdown. This server aggressively defends against automated copying for any reason including offline viewing, duplication, etc... Please respect this requirement and DO NOT RIP THIS SITE. Questions? <A HREF="http://sxlist.com/techref/inet/iis/jscript/htm/js528.htm"> Array Object</A> |
Did you find what you needed? |
Welcome to sxlist.com!sales, advertizing, & kind contributors just like you! Please don't rip/copy (here's why Copies of the site on CD are available at minimal cost. |
Welcome to sxlist.com! |
.