[ Web Proxy ]
URL:
Viewing: https://ar.javascript.info/array [Back]  [Original]

:
Light themeDark theme
DanskEnglishEspaolFranaisIndonesiaItalianoTrkeOzbek

. .

, . , : , , 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). / / .

deque.

:

pop : :

```js run
 ;["", "", ""] = let fruits

 ;alert( fruits.pop() ) //   ""    

 ;alert( fruits ) // , 
```
push

:

let fruits = ["", ""];

fruits.push("");

alert( fruits ); // , , 

(...)fruits.push fruits[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 :

  1. 0.
  2. , 1 0, 2 1 .
  3. 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] ); // , , 
}

. :

  1. for ... in * * .

    , . length , , . for..in . .

  2. for..in 10-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 null and undefined that 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).

let fruits = ["", "", ""];

// "   "
let shoppingCart = fruits;
shoppingCart.push("");

// fruits  
alert( fruits.length ); // ?

4:

let fruits = ["", "", ""];

let shoppingCart = fruits;

shoppingCart.push("");

alert( fruits.length ); // 4

. shoppingCart fruits .

5 .

  1. styles .
  2. " "
  3. . .
  4. .
  5. Rap Reggae .

:

 , 
 , ,    
 , ,    
,   
 ,  , ,    
let styles = ["", " "];
styles.push("   ");
styles[Math.floor((styles.length - 1) / 2)] = "";
alert( styles.shift() );
styles.unshift(" ", " ");

let arr = ["a", "b"];

arr.push(function() {
  alert( this );
})

arr[2](); // ?

()arr[2] ()obj[method], obj arr, method 2.

arr [2] . this arr :

let arr = ["a", "b"];

arr.push(function() {
  alert( this );
})

arr[2](); // a,b,function(){...}

3 : function.

sumInput() :

  • prompt .
  • .

. 0 .

. value prompt, value = +value ( ) ( ). .

function sumInput() {

  let numbers = [];

  while (true) {

    let value = prompt("     A Number Please", 0);

    //   
    if (value === "" || value === null || !isFinite(value)) break;

    numbers.push(+value);
  }

  let sum = 0;
  for (let number of numbers) {
    sum += number;
  }
  return sum;
}

alert( sumInput() );

arr = [1, -2, 3, 4, -9, 6].

: arr .

getMaxSubSum(arr) .

:

getMaxSubSum([-1, 2, 3, -9]) == 5 (  )
getMaxSubSum([2, -1, 2, 3, -9]) == 6
getMaxSubSum([-1, 2, 3, -9, 11]) == 11
getMaxSubSum([-2, -1, 1, 2]) == 3
getMaxSubSum([100, -9, 2, -3, 5]) == 100
getMaxSubSum([1, 2, 3]) == 6 ( )

( ) :

getMaxSubSum([-1, -2, -3]) = 0

: O(n2) O (n) .

sandbox .

.

.

, for [-1, 2, 3, -9, 11]:

//   -1:
-1 - 1 + 2 - 1 + 2 + 3 - 1 + 2 + 3 + -9 - 1 + 2 + 3 + -9 + 11;

//   2:
2;
2 + 3;
2 + 3 + -9;
2 + 3 + -9 + 11;

//   3:
3;
3 + -9;
3 +
  -9 +
  11 -
  //   -9
  9 -
  9 +
  11;

//   11
11;

: .

function getMaxSubSum(arr) {
  let maxSum = 0; //         

  for (let i = 0; i < arr.length; i++) {
    let sumFixedStart = 0;
    for (let j = i; j < arr.length; j++) {
      sumFixedStart += arr[j];
      maxSum = Math.max(maxSum, sumFixedStart);
    }
  }

  return maxSum;
}

alert(getMaxSubSum([-1, 2, 3, -9])); // 5
alert(getMaxSubSum([-1, 2, 3, -9, 11])); // 11
alert(getMaxSubSum([-2, -1, 1, 2])); // 3
alert(getMaxSubSum([1, 2, 3])); // 6
alert(getMaxSubSum([100, -9, 2, -3, 5])); // 100

The solution has a time complexity of O(n2). In other words, if we increase the array size 2 times, the algorithm will work 4 times longer.

s. s s = 0. .

:

function getMaxSubSum(arr) {
  let maxSum = 0;
  let partialSum = 0;

  for (let item of arr) {
    //    
    partialSum += item; //    
    maxSum = Math.max(maxSum, partialSum); //   
    if (partialSum < 0) partialSum = 0; //    
  }

  return maxSum;
}

alert(getMaxSubSum([-1, 2, 3, -9])); // 5
alert(getMaxSubSum([-1, 2, 3, -9, 11])); // 11
alert(getMaxSubSum([-2, -1, 1, 2])); // 3
alert(getMaxSubSum([100, -9, 2, -3, 5])); // 100
alert(getMaxSubSum([1, 2, 3])); // 6
alert(getMaxSubSum([-1, -2, -3])); // 0

O (n).

: Maximum subarray problem. .

sandbox.


Web Proxy Viewer  |  New URL  |  Original Page