. .
, . , : , , HTML .
, . . .
Array, .
:
let arr = new Array();
let arr = [];
. :
let fruits = ["", "", ""];
. :
let fruits = ["", "", ""];
alert( fruits[0] ); //
alert( fruits[1] ); //
alert( fruits[2] ); //
:
fruits[2] = ''; // ["", "", ""]
:
fruits[3] = ''; // ["", "", "", ""]
length:
let fruits = ["", "", ""];
alert( fruits.length ); // 3
alert .
let fruits = ["", "", ""];
alert( fruits ); // ,,
.
:
//
let arr = [ '', { name: '' }, true, function() { alert(''); } ];
// 1
alert( arr[1].name ); //
// 3
arr[3](); //
:
let fruits = [
"",
"",
"",
];
/ .````
pop/push, shift/unshift
push.shift.
. . . stack.
:
push.pop.
.
(stack) : :
(stacks), LIFO (Last-In-First-Out). FIFO (First-In-First-Out).
JavaScript (stack). / / .
:
pop
: :
```js run
;["", "", ""] = let fruits
;alert( fruits.pop() ) // ""
;alert( fruits ) // ,
```
push-
:
let fruits = ["", ""]; fruits.push(""); alert( fruits ); // , ,(...)fruits.pushfruits[fruits.length] = ....
:
shift-
:
let fruits = ["", "", ""]; alert( fruits.shift() ); // alert( fruits ); // , unshift-
:
let fruits = ["", ""]; fruits.unshift(''); alert( fruits ); // , ,
push unshift :
let fruits = [""];
fruits.push("", "");
fruits.unshift("", "");
// ["", "", "", "", ""]
alert( fruits );
. arr[0] . obj[key], arr , .
Remember, there are only eight basic data types in JavaScript (see the Data types chapter for more info). Array is an object and thus behaves like an object.
, :
let fruits = [""]
let arr = fruits; // ( )
alert( arr === fruits ); //
arr.push(""); //
alert( fruits ); // - 2
But what makes arrays really special is their internal representation. The engine tries to store its elements in the contiguous memory area, one after another, just as depicted on the illustrations in this chapter, and there are other optimizations as well, to make arrays work really fast.
.
:
let fruits = []; //
fruits[99999] = 5; //
fruits.age = 25; //
. .
. .
:
-
arr.test = 5. - , :
arr[0]arr[1000]( ). - ,
arr[1000],arr[999].
* *. . JavaScript . {}.
push/pop , shift/unshift .
:
fruits.shift(); //
0. .
shift 3 :
-
0. - ,
10,21. -
length.
, , .
unshift: , , .
push/pop? . , pop length.
pop :
fruits.pop(); //
** pop , . .**
push .
for :
let arr = ["", "", ""];
for (let i = 0; i < arr.length; i++) {
alert( arr[i] );
}
, for..of:
let fruits = ["", "", ""];
//
for (let fruit of fruits) {
alert( fruit );
}
The for..of , , . .
for..in:
let arr = ["", "", ""];
for (let key in arr) {
alert( arr[key] ); // , ,
}
. :
-
for ... in* * ., .
length, , .for..in. . -
for..in10-100 . . . .
forin .
length
length . .
:
let fruits = [];
fruits[123] = "";
alert( fruits.length ); // 124
.
length .
. . :
let arr = [1, 2, 3, 4, 5];
arr.length = 2; //
alert( arr ); // [1, 2]
arr.length = 5; //
alert( arr[3] ); // :
: ;arr.length = 0.
()new Array
:
let arr = new Array("", "", "");
`[] . .
* *.
:
let arr = new Array(2); // [2] ?
alert( arr[0] ); // ! .
alert( arr.length ); // 2
, () .
.
. :
let matrix = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
];
alert( matrix[1][1] ); // 5,
toString .
:
let arr = [1, 2, 3];
alert( arr ); // 1,2,3
alert( String(arr) === '1,2,3' ); //
:
alert( [] + 1 ); // "1"
alert( [1] + 1 ); // "11"
alert( [1,2] + 1 ); // "1,21"
Symbol.toPrimitive valueOf toString [] [1] "" 1 "" "[ 12]" 12 ".
the binaryn plus + :
alert( "" + 1 ); // "1"
alert( "1" + 1 ); // "11"
alert( "1,2" + 1 ); // "1,21"
Dont compare arrays with ==
Arrays in JavaScript, unlike some other programming languages, shouldnt be compared with operator ==.
This operator has no special treatment for arrays, it works with them as with any objects.
Lets recall the rules:
- Two objects are equal
==only if theyre references to the same object. - If one of the arguments of
==is an object, and the other one is a primitive, then the object gets converted to primitive, as explained in the chapter . - With an exception of
nullandundefinedthat equal==each other and nothing else.
The strict comparison === is even simpler, as it doesnt convert types.
So, if we compare arrays with ==, they are never the same, unless we compare two variables that reference exactly the same array.
For example:
alert( [] == [] ); // false
alert( [0] == [0] ); // false
These arrays are technically different objects. So they arent equal. The == operator doesnt do item-by-item comparison.
Comparison with primitives may give seemingly strange results as well:
alert( 0 == [] ); // true
alert('0' == [] ); // false
Here, in both cases, we compare a primitive with an array object. So the array [] gets converted to primitive for the purpose of comparison and becomes an empty string ''.
Then the comparison process goes on with the primitives, as described in the chapter :
// after [] was converted to ''
alert( 0 == '' ); // true, as '' becomes converted to number 0
alert('0' == '' ); // false, no type conversion, different strings
So, how to compare arrays?
Thats simple: dont use the == operator. Instead, compare them item-by-item in a loop or using iteration methods explained in the next chapter.
Summary
.
-
:
// () let arr = [2, 1...]; // ( ) let arr = new Array(2, 1...);" () "
-
length. . -
.
:
push(...).pop().shift().unshift(...).
:
for (let i=0; i<arr.length; i++)-- .for (let item of arr)for (let i in arr).
To compare arrays, dont use the == operator (as well as >, < and others), as they have no special treatment for arrays. They handle them as any objects, and its not what we usually want.
Instead you can use for..of loop to compare arrays item-by-item.
We will continue with arrays and study more methods to add, remove, extract elements and sort arrays in the next chapter (Array methods).
<code><pre>10 (plnkr, JSBin, codepen)