๐ง๐ป๐ป ๊ฐ๋ฐ๊ฐ๋ฐ/javascript
๋ฐฐ์ด ์์ ์ถ๊ฐ ๋ฐ ์ญ์ , ๋ฐฐ์ด ์์ ํฌํจ ์ฌ๋ถ ํ์ธ/ koans
Seungjae Lee
2021. 5. 27. 09:38
1. ๋ฐฐ์ด ์์ ์ถ๊ฐ ๋ฐ ์ญ์
[].push() : ๋ฐฐ์ด์ ๋์ ์์(element) ์ถ๊ฐ
let arr = ['seung', 'jae', 'lee']
arr.push('good')
console.log(arr)
// ["seung", "jae", "lee", "good"]
[].pop() : ๋ฐฐ์ด์ ๋์ ์์(element) ์ญ์
console.log(arr)
["seung", "jae", "lee", "good"]
arr.pop()
console.log(arr)
// ["seung", "jae", "lee"]
[].unshift() : ๋ฐฐ์ด์ ๋งจ ์์ชฝ์ ์์(element) ์ถ๊ฐ
console.log(arr)
//["seung", "jae", "lee"]
arr.unshift('nice')
console.log(arr)
//["nice", "seung", "jae", "lee"]
[].shift() : ๋ฐฐ์ด์ ์ฒซ ๋ฒ์งธ ์์(element) ์ญ์
console.log(arr)
//["nice", "seung", "jae", "lee"]
arr.shift()
console.log(arr)
//["seung", "jae", "lee"]
2. ๋ฐฐ์ด ์์ ํฌํจ ์ฌ๋ถ ํ์ธ
[].indexOf() : ๋ฐฐ์ด์์ ํน์ ๊ฐ์ index๋ฅผ ์ฐพ์์ ๋ฐํ(์ฒซ ๋ฒ์งธ ๊ฐ), ์กด์ฌํ์ง ์์ผ๋ฉด -1์ ๋ฐํ
let arr = ["nice", "seung", "jae", "lee"]
arr.indexOf("seung")
// 1
arr.indexOf("Seung")
// -1
arr.indexOf("seung") !== -1
// true
arr.indexOf("molla") !== -1
// false
[]. includes() : ๋ฐฐ์ด์ ํน์ ์์๋ฅผ ํฌํจํ๊ณ ์๋์ง ํ๋ณ(๋ธ๋ผ์ฐ์ ํธํ์ฑ : Internet Explorer ๋ฅผ ์ง์ํ์ง ์์)
arr.includes("seung")
//true
arr.includes("Seung")
//false
Koans
describe('Array์ ๋ํด์ ํ์ตํฉ๋๋ค.', function () {
it('Array์ ๊ธฐ๋ณธ์ ํ์ธํฉ๋๋ค.', function () {
const emptyArr = [];
expect(typeof emptyArr === 'array').to.equal(false);
expect(emptyArr.length).to.equal(0);
const multiTypeArr = [
0,
1,
'two',
function () {
return 3;
},
{ value1: 4, value2: 5 },
[6, 7],
];
expect(multiTypeArr.length).to.equal(6);
expect(multiTypeArr[0]).to.equal(0);
expect(multiTypeArr[2]).to.equal('two');
expect(multiTypeArr[3]()).to.equal(3);
expect(multiTypeArr[4].value1).to.equal(4);
expect(multiTypeArr[4]['value2']).to.equal(5);
expect(multiTypeArr[5][1]).to.equal(7);
});
it('Array์ ์์(element)๋ฅผ ๋ค๋ฃจ๋ ๋ฐฉ๋ฒ์ ํ์ธํฉ๋๋ค.', function () {
const arr = [];
expect(arr).to.deep.equal([]);
arr[0] = 1;
expect(arr).to.deep.equal([1]);
arr[1] = 2;
expect(arr).to.deep.equal([1, 2]);
arr.push(3);
expect(arr).to.deep.equal([1, 2, 3]);
const poppedValue = arr.pop();
expect(poppedValue).to.equal(3);
expect(arr).to.deep.equal([1, 2]);
});
it('Array ๋ฉ์๋ slice๋ฅผ ํ์ธํฉ๋๋ค.', function () {
const arr = ['peanut', 'butter', 'and', 'jelly'];
expect(arr.slice(1)).to.deep.equal(['butter', 'and', 'jelly']);
expect(arr.slice(0, 1)).to.deep.equal(['peanut']);
expect(arr.slice(0, 2)).to.deep.equal(['peanut', 'butter']);
expect(arr.slice(2, 2)).to.deep.equal([]);
expect(arr.slice(2, 20)).to.deep.equal(['and', 'jelly']);
expect(arr.slice(3, 0)).to.deep.equal([]);
expect(arr.slice(3, 100)).to.deep.equal(['jelly']);
expect(arr.slice(5, 1)).to.deep.equal([]);
// arr.slice๋ arr์ ๊ฐ์ ๋ณต์ฌํ์ฌ ์๋ก์ด ๋ฐฐ์ด์ ๋ฆฌํดํฉ๋๋ค.
// ์๋์ ์ฝ๋๋ arr ์ ์ฒด๋ฅผ ๋ณต์ฌํฉ๋๋ค. ์์ฃผ ์ฌ์ฉ๋๋ ๊ธฐ์ตํ์๊ธฐ ๋ฐ๋๋๋ค.
expect(arr.slice(0)).to.deep.equal(['peanut', 'butter', 'and', 'jelly']);
});
it('Array๋ฅผ ํจ์์ ์ธ์๋ก ์ ๋ฌํ ๊ฒฝ์ฐ, reference๊ฐ ์ ๋ฌ๋ฉ๋๋ค.', function () {
// call(pass) by value์ call(pass) by reference์ ์ฐจ์ด์ ๋ํด์ ํ์ตํฉ๋๋ค.
const arr = ['zero', 'one', 'two', 'three', 'four', 'five'];
function passedByReference(refArr) {
refArr[1] = 'changed in function';
}
passedByReference(arr);
expect(arr[1]).to.equal('changed in function');
const assignedArr = arr;
assignedArr[5] = 'changed in assignedArr';
expect(arr[5]).to.equal('changed in assignedArr');
const copiedArr = arr.slice();
copiedArr[3] = 'changed in copiedArr';
expect(arr[3]).to.equal('three');
});
it('Array ๋ฉ์๋ shift์ unshift๋ฅผ ํ์ธํฉ๋๋ค.', function () {
const arr = [1, 2];
arr.unshift(3);
expect(arr).to.deep.equal([3, 1, 2]);
const shiftedValue = arr.shift();
expect(shiftedValue).to.deep.equal(3);
expect(arr).to.deep.equal([1, 2]);
});
});