자바스크립트(javascript) 배열(array) 사용

Language/JAVASCRIPT 2011. 7. 27. 14:27

1. 배열생성

var array_test = new Array();

 

2. push 메소드 사용

Definition and Usage

The push() method adds new elements to the end of an array, and returns the new length.

Note: This method changes the length of an array!

Syntax

array.push(element1, element2, ..., elementX)

ParameterDescription
element1, element2, ..., elementX Required. The element(s) to add to the end of the array

Example

Example

Add new elements to the end of an array, and return the new length:

<script type="text/javascript">

var fruits = ["Banana", "Orange", "Apple", "Mango"];
document.write(fruits.push("Kiwi") + "<br />");
document.write(fruits.push("Lemon","Pineapple") + "<br />");
document.write(fruits);

</script>

The output of the code above will be:

5
7
Banana,Orange,Apple,Mango,Kiwi,Lemon,Pineapple

 

 

3. join 메소드 사용

Definition and Usage

The join() method joins all elements of an array into a string, and returns the string.

The elements will be separated by a specified separator. The default separator is comma (,).

Syntax

array.join(separator)

ParameterDescription
separator Optional. The separator to be used. If omitted, the elements are separated with a comma

Example

Example

Join all elements of an array into a string:

<script type="text/javascript">

var fruits = ["Banana", "Orange", "Apple", "Mango"];
document.write(fruits.join() + "<br />");
document.write(fruits.join("+") + "<br />");
document.write(fruits.join(" and "));

</script>

The output of the code above will be:

Banana,Orange,Apple,Mango
Banana+Orange+Apple+Mango
Banana and Orange and Apple and Mango

 

 

출처 - http://www.w3schools.com/jsref/jsref_obj_array.asp

 

JavaScript Array Object


Array Object

The Array object is used to store multiple values in a single variable.

For a tutorial about Arrays, read our JavaScript Array Object tutorial.


Array Object Properties

PropertyDescription
constructor Returns the function that created the Array object's prototype
length Sets or returns the number of elements in an array
prototype Allows you to add properties and methods to an object

Array Object Methods

MethodDescription
concat() Joins two or more arrays, and returns a copy of the joined arrays
indexOf()  
join() Joins all elements of an array into a string
pop() Removes the last element of an array, and returns that element
push() Adds new elements to the end of an array, and returns the new length
reverse() Reverses the order of the elements in an array
shift() Removes the first element of an array, and returns that element
slice() Selects a part of an array, and returns the new array
sort() Sorts the elements of an array
splice() Adds/Removes elements from an array
toString() Converts an array to a string, and returns the result
unshift() Adds new elements to the beginning of an array, and returns the new length
valueOf() Returns the primitive value of an array


- 출처 : http://blog.naver.com/fromyongsik

: