1. 배열의 선언 및 접근, 속성
[라인 2] 와 같이 명시적으로 Array 오브젝트를 생성할 수도 있고, [라인 8] 같이 단순하게 선언하고 사용할 수도 있다. [라인 20] 과 같이 배열의 요소에 자바스크립트의 어떤 타입이라도 사용이 가능하다. [라인 14, 15] 에서처럼 index 로 int 형이 아닌 다른 타입을 주게 되면, 오브젝트의 속성으로 생성이 된다.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
it("delcare array, access element, property", function() { | |
var aray = new Array(3); | |
expect(aray.length).toBe(3); | |
aray = Array(4); | |
expect(aray.length).toBe(4); | |
aray = []; | |
aray.length = 5; | |
expect(aray.length).toBe(5); | |
aray[0] = 0; | |
aray[1] = 1; | |
aray[1.5] = "this prop"; | |
aray["prop"] = "this is prop"; | |
expect(aray.length).toBe(5); | |
expect(aray[1.5]).toBe("this prop"); | |
expect(aray.hasOwnProperty(1.5)).toBe(true); | |
aray = new Array(1,2,3,"hi"); | |
expect(aray.length).toBe(4); | |
expect(aray[0]).toBe(1); | |
expect(aray[3]).toBe("hi"); | |
aray = [1, 2, "hi"]; | |
expect(aray.length).toBe(3); | |
expect(aray[0]).toBe(1); | |
expect(aray[2]).toBe("hi"); | |
expect(aray["length"]).toBe(3); | |
}); |
[라인 5]에서 알 수 있듯이 자바스크립트의 length 는 단순히 가장 마지막 index + 1 이다.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
it("length of array" , function() { | |
//length of array is just last index + 1 | |
var aray = []; | |
aray[10] = 10; | |
expect(aray.length).toBe(11); | |
aray = ["a", "b", "c"]; | |
expect(aray.length).toBe(3); | |
aray.length = 2; | |
expect(aray.length).toBe(2); | |
expect(aray[0]).toBe("a"); | |
expect(aray[1]).toBe("b"); | |
expect(aray[2]).toBe(undefined); | |
aray.length = 3; | |
expect(aray[2]).toBe(undefined); | |
}); |
for, for-each을 통해서 배열의 요소들을 순회할 수 있다. 물론 for-in 구문도 사용이 가능하다.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
it("iterations of array", function() { | |
var aray = ["1", "2", "3"]; | |
for (var i = 0; i < aray.length; i++) { | |
expect(aray[i]).toBe((i + 1).toString()); | |
} | |
var sum = 0; | |
aray.forEach(function(i) { | |
sum += (+i); | |
}) | |
expect(sum).toBe(6); | |
}); | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var aray = [1, 2, 3]; | |
//concat | |
aray = aray.concat(4, 5, 6); | |
expect(aray.length).toBe(6); | |
for (var i = 0; i < aray.lenght; i++) { | |
expect(aray[i]).toBe(i + 1); | |
} | |
//push | |
aray = [1, 2, 3]; | |
expect(aray.length).toBe(3); | |
aray.push(4); | |
expect(aray.length).toBe(4); | |
expect(aray[3]).toBe(4); | |
//pop | |
expect(aray.pop()).toBe(4); | |
expect(aray.length).toBe(3); | |
//join | |
expect(aray.join(",")).toBe("1,2,3"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//shift | |
expect(aray.shift()).toBe(1); | |
expect(aray.shift()).toBe(2); | |
expect(aray.shift()).toBe(3); | |
expect(aray.length).toBe(0); | |
//unshift | |
aray = [1, 2, 3]; | |
aray.unshift(4, 5); | |
expect(aray.length).toBe(5); | |
expect(aray[0]).toBe(4); | |
expect(aray[1]).toBe(5); | |
expect(aray[2]).toBe(1); | |
expect(aray[3]).toBe(2); | |
expect(aray[4]).toBe(3); |
splice 라는 함수는 배열의 특정 구간의 요소들을 지우고, 거기에 새로운 요소들을 삽입하는 함수이다. 배열 [1,
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//slice | |
aray = [1, 2, 3, 4, 5] | |
aray = aray.slice(0, 2); | |
expect(aray.length).toBe(2); | |
expect(aray[0]).toBe(1); | |
expect(aray[1]).toBe(2); | |
//splice(startInx, countToRemove, addElement, addElement, ...) | |
aray = [1, 2, 3, 4, 5]; | |
aray.splice(1, 2, "a", "b", "c"); | |
console.log(aray); | |
expect(aray.length).toBe(6); | |
expect(aray[0]).toBe(1); | |
expect(aray[1]).toBe("a"); | |
expect(aray[2]).toBe("b"); | |
expect(aray[3]).toBe("c"); | |
expect(aray[4]).toBe(4); | |
expect(aray[5]).toBe(5); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//reverse | |
aray = [1, 2, 3]; | |
aray.reverse(); | |
expect(aray[0]).toBe(3); | |
expect(aray[1]).toBe(2); | |
expect(aray[2]).toBe(1); | |
//sort | |
aray = [5, 1, 3, 6]; | |
aray.sort(); | |
expect(aray[0]).toBe(1); | |
expect(aray[1]).toBe(3); | |
expect(aray[2]).toBe(5); | |
expect(aray[3]).toBe(6); | |
aray = [5, 1, 3, 6]; | |
var sortFunc = function(a, b) { | |
if (a > b) {return -1;} | |
else if ( a < b) { return 1;} | |
else {return 0;} | |
} | |
aray.sort(sortFunc); | |
expect(aray[0]).toBe(6); | |
expect(aray[1]).toBe(5); | |
expect(aray[2]).toBe(3); | |
expect(aray[3]).toBe(1); |
lastIndexOf는 배열의 마지막에서부터 검사를 한다.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//index of | |
aray = ["a", "b", "c", "a", "a"]; | |
expect(aray.indexOf("a")).toBe(0); | |
expect(aray.indexOf("a", 1)).toBe(3); | |
expect(aray.indexOf("F")).toBe(-1); | |
//lastindex of | |
expect(aray.lastIndexOf("a")).toBe(4); | |
expect(aray.lastIndexOf("a", 2)).toBe(0); |
배열의 각각의 요소를 인자로 받아 특정 작업을 하고자 할 때 유용하다.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//forEach(callback) | |
aray = [1, 2, 3]; | |
aray.forEach(function(a) { | |
var inx = aray.indexOf(a); | |
aray[inx] = a * a; | |
}); | |
expect(aray[0]).toBe(1); | |
expect(aray[1]).toBe(4); | |
expect(aray[2]).toBe(9); | |
//map(callback) | |
aray = ["a", "b", "c"]; | |
var aray2 = aray.map(function(item) { | |
return item.toUpperCase(); | |
}); | |
expect(aray2[0]).toBe("A"); | |
expect(aray2[1]).toBe("B"); | |
expect(aray2[2]).toBe("C") | |
aray = [1, "a", 2, "b"]; | |
aray2 = aray.filter(function(item) { | |
return (typeof item == "number"); | |
}) | |
expect(aray2.length).toBe(2) | |
expect(aray2[0]).toBe(1); | |
expect(aray2[1]).toBe(2); |
배열 전체나 일부구간에 대한 검사등과 같은 작업을 할 때 유용하다.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//every | |
aray = [1, 2, 3]; | |
var result = aray.every(function(item) { | |
return (typeof item == "number"); | |
}); | |
expect(result).toBe(true); | |
//some | |
result = aray.some(function(item) { | |
return (item % 2) == 0; | |
}); | |
expect(result).toBe(true); | |
//reduce | |
aray = [1, 2, 3]; | |
result = aray.reduce(function(a, b) { | |
return a + b; | |
}); | |
expect(result).toBe(6); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
it("Array generic method", function() { | |
var string = "hello"; | |
var aray = new Array(); | |
Array.prototype.forEach.call(string, function(char) { | |
aray.push(char); | |
}); | |
expect(aray[0]).toBe('h'); | |
expect(aray[1]).toBe('e'); | |
expect(aray[2]).toBe('l'); | |
expect(aray[3]).toBe('l'); | |
expect(aray[4]).toBe('o'); | |
}); |
12. 참조
- MDN - https://developer.mozilla.org/en/docs/Web/JavaScript
- Jasmine - http://jasmine.github.io/2.3/introduction.html
댓글 없음:
댓글 쓰기