JavaScript-배열 객체

그만큼 Array개체를 사용하면 단일 변수에 여러 값을 저장할 수 있습니다. 동일한 유형의 요소의 고정 크기 순차 컬렉션을 저장합니다. 배열은 데이터 모음을 저장하는 데 사용되지만 배열을 동일한 유형의 변수 모음으로 생각하는 것이 더 유용합니다.

통사론

다음 구문을 사용하여 Array 객체-

var fruits = new Array( "apple", "orange", "mango" );

그만큼 Array매개 변수는 문자열 또는 정수 목록입니다. Array 생성자를 사용하여 단일 숫자 매개 변수를 지정할 때 배열의 초기 길이를 지정합니다. 배열에 허용되는 최대 길이는 4,294,967,295입니다.

다음과 같이 값을 할당하여 배열을 만들 수 있습니다.

var fruits = [ "apple", "orange", "mango" ];

서수를 사용하여 다음과 같이 배열 내부의 값에 액세스하고 설정합니다.

fruits[0] is the first element
fruits[1] is the second element
fruits[2] is the third element

배열 속성

다음은 해당 설명과 함께 Array 객체의 속성 목록입니다.

Sr. 아니. 속성 및 설명
1 constructor

Returns a reference to the array function that created the object.

2

index

The property represents the zero-based index of the match in the string

3

input

This property is only present in arrays created by regular expression matches.

4 length

Reflects the number of elements in an array.

5 prototype

The prototype property allows you to add properties and methods to an object.

In the following sections, we will have a few examples to illustrate the usage of Array properties.

Array Methods

Here is a list of the methods of the Array object along with their description.

Sr.No. Method & Description
1 concat()

Returns a new array comprised of this array joined with other array(s) and/or value(s).

2 every()

Returns true if every element in this array satisfies the provided testing function.

3 filter()

Creates a new array with all of the elements of this array for which the provided filtering function returns true.

4 forEach()

Calls a function for each element in the array.

5 indexOf()

Returns the first (least) index of an element within the array equal to the specified value, or -1 if none is found.

6 join()

Joins all elements of an array into a string.

7 lastIndexOf()

Returns the last (greatest) index of an element within the array equal to the specified value, or -1 if none is found.

8 map()

Creates a new array with the results of calling a provided function on every element in this array.

9 pop()

Removes the last element from an array and returns that element.

10 push()

Adds one or more elements to the end of an array and returns the new length of the array.

11 reduce()

Apply a function simultaneously against two values of the array (from left-to-right) as to reduce it to a single value.

12 reduceRight()

Apply a function simultaneously against two values of the array (from right-to-left) as to reduce it to a single value.

13 reverse()

Reverses the order of the elements of an array -- the first becomes the last, and the last becomes the first.

14 shift()

Removes the first element from an array and returns that element.

15 slice()

Extracts a section of an array and returns a new array.

16 some()

Returns true if at least one element in this array satisfies the provided testing function.

17 toSource()

Represents the source code of an object

18 sort()

Sorts the elements of an array

19 splice()

Adds and/or removes elements from an array.

20 toString()

Returns a string representing the array and its elements.

21 unshift()

Adds one or more elements to the front of an array and returns the new length of the array.

In the following sections, we will have a few examples to demonstrate the usage of Array methods.