ํ๋ฐ ๊ณผ์ ๋ฅผ ์ํด์๋ underscore.js์ฒ๋ผ underbar๋ฅผ ์ฌ์ฉํ๋ค...!
๋ฏธ๋ฆฌ๋ฏธ๋ฆฌ ์ฐ์ตํด๋์ :)
npm install underscore
const _ = require('underscore')
underscore๋ฅผ htmlํ์ผ์์ script๋ก ๋ฐ์์ ์ฌ์ฉํ๋ ๋ฐฉ๋ฒ๋ ์์ง๋ง..๋ถํธํ๋๊น...๐ ๐ปโ
๋ฐฐ์ด์ ์ฒซ ๋ฒ์งธ ์์๋ฅผ ๋ฆฌํด.
const _ = require('underscore'); // Immutable
const numArr = [3,2,1,4,5];
const korArr = ['๊ฐ','๋','๋ค','๋ผ','๋ง'];
console.log(_.first(numArr)); // 1
console.log(_.first(numArr, 3)); // [ 3, 2, 1 ]
๋ง์ง๋ง ์์๋ฅผ ์ ์ธํ ๋ฐฐ์ด์ ๋ฆฌํด
console.log(_.initial(korArr)); // [ '๊ฐ', '๋', '๋ค', '๋ผ' ]
console.log(_.initial(korArr, 3)) // [ '๊ฐ', '๋', '๋ค', '๋ผ' ]
๋ง์ง๋ง ์์๋ฅผ ๋ฆฌํด
console.log(_.last(korArr)); // '๋ง'
console.log(_.last(korArr, 3)); // [ '๋ค', '๋ผ', '๋ง' ]
๋ง์ง๋ง ์์๋ฅผ ๋ฆฌํด
console.log(_.last(korArr)); // '๋ง'
console.log(_.last(korArr, 3)); // [ '๋ค', '๋ผ', '๋ง' ]
index์ดํ ๋ถํฐ ๋ฆฌํด
console.log(_.rest(korArr)) // [ '๋', '๋ค', '๋ผ', '๋ง' ]
console.log(_.rest(korArr, 3)) // [ '๋ผ', '๋ง' ]
์ค์ฒฉ ๋ฐฐ์ด์ ํด์ ํ๋ค.
console.log(_.flatten([1, [2], [3, [[4]]]])); // [ 1, 2, 3, 4 ]
console.log(_.flatten([1, [2], [3, [[4]]]], true)); // [ 1, 2, 3, [ [ 4 ] ] ]
values๋ฅผ ์ ๊ฑฐํ ์๋ก์ด ๋ฐฐ์ด์ ๋ฆฌํด
console.log(_.without([1, 2, 1, 0, 3, 1, 4], 0, 1)); // [ 2, 3, 4 ]
console.log(_.without(korArr, '๋', '๋ง')); // [ '๊ฐ', '๋ค', '๋ผ' ]
ํฉ์งํฉ
console.log(_.union([1, 2, 3], [101, 2, 1, 10], [2, 1])); // [ 1, 2, 3, 101, 10 ]
๊ต์งํฉ
console.log(_.intersection([1, 2, 3], [101, 2, 1, 10], [2, 1])); // [ 1, 2 ]
์ฐจ์งํฉ
console.log(_.difference([1, 2, 3, 4, 5], [5, 2, 10])); // [ 1, 3, 4 ]
๋ฐฐ์ด์์ value๋ฅผ ์ฐพ์ index๋ฅผ ๋ฆฌํด
console.log(_.indexOf([1, 2, 3], 2)); // 1
start~stop๊น์ง step๋งํผ ๋ฐฐ์ด ์์ฑ
console.log(_.range(0, 10)) // [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ]
console.log(_.range(0, 10, 2)) // [ 0, 2, 4, 6, 8 ]
console.log(_.range(0, -10, -1)) // [ 0, -1, -2, -3, -4, -5, -6, -7, -8, -9 ]
console.log(_.range(0)) // []