๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿ’ป๊ฐœ๋ฐœ

JavaScript์—์„œ ๋ฐฐ์—ด ์ค‘๋ณต ์ œ๊ฑฐํ•˜๋Š” 5๊ฐ€์ง€ ๋ฐฉ๋ฒ•

by ๋ˆˆ๋ˆ„ :) 2025. 1. 13.

JavaScript์—์„œ ๋ฐฐ์—ด์˜ ์ค‘๋ณต๋œ ์š”์†Œ๋ฅผ ์ œ๊ฑฐํ•˜๋Š” ๊ฒƒ์€ ๋งค์šฐ ํ”ํ•œ ์ž‘์—…์ž…๋‹ˆ๋‹ค. ๊ฐ๊ฐ์˜ ๋ฐฉ๋ฒ•์—๋Š” ์žฅ๋‹จ์ ์ด ์žˆ์œผ๋‹ˆ, ์ƒํ™ฉ์— ๋งž๋Š” ์ตœ์ ์˜ ๋ฐฉ๋ฒ•์„ ์„ ํƒํ•˜์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค.

1. Set ๊ฐ์ฒด ์‚ฌ์šฉํ•˜๊ธฐ

๊ฐ€์žฅ ํ˜„๋Œ€์ ์ด๊ณ  ๊น”๋”ํ•œ ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค.

const numbers = [1, 2, 2, 3, 3, 4, 5, 5]; const unique_numbers = [...new Set(numbers)]; console.log(unique_numbers); // [1, 2, 3, 4, 5]

์žฅ์ :

  • ์ฝ”๋“œ๊ฐ€ ๊ฐ„๊ฒฐํ•˜๊ณ  ์ฝ๊ธฐ ์‰ฝ์Šต๋‹ˆ๋‹ค
  • ์„ฑ๋Šฅ์ด ์šฐ์ˆ˜ํ•ฉ๋‹ˆ๋‹ค
  • ES6+ ํ™˜๊ฒฝ์—์„œ ๊ถŒ์žฅ๋˜๋Š” ๋ฐฉ์‹์ž…๋‹ˆ๋‹ค

๋‹จ์ :

  • IE์—์„œ๋Š” ์ง€์›๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค (ํด๋ฆฌํ•„ ํ•„์š”)

2. filter() ๋ฉ”์„œ๋“œ ํ™œ์šฉํ•˜๊ธฐ

๋ฐฐ์—ด์˜ ์ธ๋ฑ์Šค๋ฅผ ๋น„๊ตํ•˜๋Š” ๋ฐฉ์‹์ž…๋‹ˆ๋‹ค.

const fruits = ['apple', 'banana', 'apple', 'orange', 'banana']; const unique_fruits = fruits.filter((item, index) => { return fruits.indexOf(item) === index; }); console.log(unique_fruits); // ['apple', 'banana', 'orange']

์žฅ์ :

  • ๋ธŒ๋ผ์šฐ์ € ํ˜ธํ™˜์„ฑ์ด ์ข‹์Šต๋‹ˆ๋‹ค
  • ์ถ”๊ฐ€์ ์ธ ์กฐ๊ฑด์„ ๋„ฃ๊ธฐ ์‰ฝ์Šต๋‹ˆ๋‹ค

์ฃผ์˜์‚ฌํ•ญ:

  • ๋ฐฐ์—ด์ด ํฐ ๊ฒฝ์šฐ ์„ฑ๋Šฅ์ด ์ €ํ•˜๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค
  • indexOf๋Š” ์ฐธ์กฐ๊ฐ’ ๋น„๊ต์‹œ ์›ํ•˜๋Š” ๊ฒฐ๊ณผ๊ฐ€ ์•ˆ๋‚˜์˜ฌ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค

3. reduce() ๋ฉ”์„œ๋“œ ์‚ฌ์šฉํ•˜๊ธฐ

๋ˆ„์‚ฐ๊ธฐ(accumulator)๋ฅผ ํ™œ์šฉํ•œ ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค.

const items = ['book', 'pen', 'book', 'notebook', 'pen']; const unique_items = items.reduce((unique, item) => { return unique.includes(item) ? unique : [...unique, item]; }, []); console.log(unique_items); // ['book', 'pen', 'notebook']

์žฅ์ :

  • ๋ณต์žกํ•œ ๋กœ์ง์„ ์ถ”๊ฐ€ํ•˜๊ธฐ ์ข‹์Šต๋‹ˆ๋‹ค
  • ๋ฉ”๋ชจ๋ฆฌ ํšจ์œจ์ ์ž…๋‹ˆ๋‹ค

๋‹จ์ :

  • ์ฝ”๋“œ๊ฐ€ ๋น„๊ต์  ๋ณต์žกํ•ฉ๋‹ˆ๋‹ค
  • ๊ฐ€๋…์„ฑ์ด ๋–จ์–ด์งˆ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค

4. Object์˜ ํ‚ค๋ฅผ ํ™œ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•

๊ฐ์ฒด์˜ ํ‚ค๋Š” ์ค‘๋ณต๋  ์ˆ˜ ์—†๋‹ค๋Š” ํŠน์„ฑ์„ ํ™œ์šฉํ•ฉ๋‹ˆ๋‹ค.

const users = ['kim', 'lee', 'kim', 'park', 'lee']; const unique_users = Object.keys(users.reduce((acc, user) => { acc[user] = true; return acc; }, {})); console.log(unique_users); // ['kim', 'lee', 'park']

์žฅ์ :

  • ๋น ๋ฅธ ๊ฒ€์ƒ‰ ์„ฑ๋Šฅ
  • ํฐ ๋ฐ์ดํ„ฐ์…‹์— ํšจ์œจ์ ์ž…๋‹ˆ๋‹ค

๋‹จ์ :

  • ๋ฌธ์ž์—ด์ด๋‚˜ ์ˆซ์ž๋งŒ ์ฒ˜๋ฆฌ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค
  • ๊ฐ์ฒด๋‚˜ ๋ฐฐ์—ด์€ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค

5. lodash ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์‚ฌ์šฉํ•˜๊ธฐ

์™ธ๋ถ€ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ํ™œ์šฉํ•œ ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค.

const data = [1, 2, 2, 3, 4, 4, 5]; const unique_data = _.uniq(data); console.log(unique_data); // [1, 2, 3, 4, 5]

์žฅ์ :

  • ๋งค์šฐ ์•ˆ์ •์ ์ž…๋‹ˆ๋‹ค
  • ๋‹ค์–‘ํ•œ ์ž๋ฃŒํ˜• ์ฒ˜๋ฆฌ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค
  • ์„ฑ๋Šฅ์ด ์ตœ์ ํ™”๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค

๋‹จ์ :

  • ์™ธ๋ถ€ ์˜์กด์„ฑ์ด ์ƒ๊น๋‹ˆ๋‹ค
  • ๋ฒˆ๋“ค ํฌ๊ธฐ๊ฐ€ ์ฆ๊ฐ€ํ•ฉ๋‹ˆ๋‹ค

์„ฑ๋Šฅ ๋น„๊ต ๐Ÿš€

๋ฐฐ์—ด ํฌ๊ธฐ๊ฐ€ ์ž‘์€ ๊ฒฝ์šฐ(1000๊ฐœ ๋ฏธ๋งŒ):

  1. Set ๊ฐ์ฒด ๋ฐฉ์‹
  2. filter() ๋ฐฉ์‹
  3. reduce() ๋ฐฉ์‹

๋ฐฐ์—ด ํฌ๊ธฐ๊ฐ€ ํฐ ๊ฒฝ์šฐ(1000๊ฐœ ์ด์ƒ):

  1. Set ๊ฐ์ฒด ๋ฐฉ์‹
  2. Object ํ‚ค ๋ฐฉ์‹
  3. lodash ๋ฐฉ์‹

๋งˆ๋ฌด๋ฆฌ

์ƒํ™ฉ์— ๋”ฐ๋ผ ์ ์ ˆํ•œ ๋ฐฉ๋ฒ•์„ ์„ ํƒํ•˜๋Š” ๊ฒƒ์ด ์ค‘์š”ํ•ฉ๋‹ˆ๋‹ค. ์ผ๋ฐ˜์ ์ธ ๊ฒฝ์šฐ์—๋Š” Set ๊ฐ์ฒด๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ๊ฐ€์žฅ ์ถ”์ฒœ๋˜๋Š” ๋ฐฉ๋ฒ•์ด์ง€๋งŒ, IE ์ง€์›์ด ํ•„์š”ํ•˜๊ฑฐ๋‚˜ ํŠน๋ณ„ํ•œ ์š”๊ตฌ์‚ฌํ•ญ์ด ์žˆ๋‹ค๋ฉด ๋‹ค๋ฅธ ๋ฐฉ๋ฒ•์„ ๊ณ ๋ คํ•ด๋ณด์„ธ์š”.

์ฝ”๋“œ์˜ ์œ ์ง€๋ณด์ˆ˜์„ฑ๊ณผ ์„ฑ๋Šฅ์„ ๋ชจ๋‘ ๊ณ ๋ คํ•˜์—ฌ, ํ”„๋กœ์ ํŠธ์˜ ํŠน์„ฑ์— ๋งž๋Š” ์ตœ์ ์˜ ๋ฐฉ๋ฒ•์„ ์„ ํƒํ•˜์‹œ๊ธฐ ๋ฐ”๋ž๋‹ˆ๋‹ค. ๐ŸŽฏ