레이블이 javascript array인 게시물을 표시합니다. 모든 게시물 표시
레이블이 javascript array인 게시물을 표시합니다. 모든 게시물 표시

2015년 9월 5일 토요일

Javascript Array

Let's see Javascript Array. There are many method related array . We'll see Javascript specific things and something is good to know. Learning test is written using Jasmine. Please refer Jasmine usage which I wrote before if you have interest it. 

1. Define array and access, attributes
You can create array object explicitly like [line 2]. It is also possible like [line 8] simply. Like [line 20], you can use any type of object as an array element. If you give other type which is not integer like [line 14, 15], it create attribute of object.

2. length
The length of array is a just last index + 1 as see in [line 5].

3. iteration
You can iterate element of array using for, for-each statement. Of course, you can use for-in statement.

4. concat, push, pop, join
5. shift, unshift
6. slice, splice
The splice function remove element which are in specific range and insert into new elements. You can see element 2, 3 are deleted from Array [1,2,3,4,5] and new element "a","b","c" are inserted. So the array contains [1,"a","b","c",4,5].

7. reverse, sort
8. indexOf, lastIndexOf
The lastIndexOf method start inspecting from last index of array.
9. forEach, map
It is useful when do some work using each element of array as an argument.
10. every, some, reduce
It is useful when do some inspecting work from whole range or specific range.
11. Array Generic Method
12. Reference
- MDN - https://developer.mozilla.org/en/docs/Web/JavaScript
- Jasmine - http://jasmine.github.io/2.3/introduction.html


2015년 8월 4일 화요일

자바스크립트 배열

자바스크립트의 배열에 대해서 살펴본다. 배열과 관련된 다양한 함수들이 많다. 학습테스트는 자바스크립트 테스트 프레임워크인 자스민을 이용해서 작성되었다. 자스민에 대한 사용법은 이 전에 작성했던 "자스민 사용법" 글을 참조하자.

1. 배열의 선언 및 접근, 속성
[라인 2] 와 같이 명시적으로 Array 오브젝트를 생성할 수도 있고, [라인 8] 같이 단순하게 선언하고 사용할 수도 있다. [라인 20] 과 같이 배열의 요소에 자바스크립트의 어떤 타입이라도 사용이 가능하다. [라인 14, 15] 에서처럼 index 로 int 형이 아닌 다른 타입을 주게 되면, 오브젝트의 속성으로 생성이 된다.

2. length
[라인 5]에서 알 수 있듯이 자바스크립트의 length 는 단순히 가장 마지막 index + 1 이다.

3. iteration
for, for-each을 통해서 배열의 요소들을 순회할 수 있다. 물론 for-in 구문도 사용이 가능하다.

4. concat, push, pop, join
5. shift, unshift
6. slice, splice
splice 라는 함수는 배열의 특정 구간의 요소들을 지우고, 거기에 새로운 요소들을 삽입하는 함수이다. 배열 [1,2,3,4,5] 에서 2,3 요소가 삭제되고 거기에 "a","b","c" 가 삽입되어 [1,"a","b","c",4,5] 가 됨을 알 수 있다.

7. reverse, sort
8. indexOf, lastIndexOf
lastIndexOf는 배열의 마지막에서부터 검사를 한다.
9. forEach, map
배열의 각각의 요소를 인자로 받아 특정 작업을 하고자 할 때 유용하다.
10. every, some, reduce
배열 전체나 일부구간에 대한 검사등과 같은 작업을 할 때 유용하다.
11. Array Generic Method
12. 참조
- MDN - https://developer.mozilla.org/en/docs/Web/JavaScript
- Jasmine - http://jasmine.github.io/2.3/introduction.html