[1]. ๐Ÿ”Ž์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ๋ฐฐ์—ด๋‚ด์žฅํ•จ์ˆ˜ ์ •๋ฆฌ (1)

JunTechยท2020๋…„ 8์›” 17์ผ
1

๐Ÿ”Š ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์˜ ๋ฐฐ์—ด๋‚ด์žฅํ•จ์ˆ˜๋ฅผ ๋ณต์Šตํ•˜๊ณ  ์ •๋ฆฌํ•˜๊ธฐ ์œ„ํ•œ ๊ธ€์ž…๋‹ˆ๋‹ค.

๐Ÿ’ก1. length

length๋Š” ๋ฐฐ์—ด์•ˆ์— '๋ช‡ ๊ฐœ'๊ฐ€ ๋“ค์–ด์žˆ๋Š”์ง€, ์ฆ‰ ๋ฐฐ์—ด์˜ ๊ธธ์ด๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. ์ฝ”๋“œ๋ฅผ ๋ณด๋ฉฐ ์ดํ•ด๋ฅผ ํ•˜๋ฉด ํŽธํ•ฉ๋‹ˆ๋‹ค.

const fruits = [apple, banana, orange];

console.log(fruits.length);

๊ฒฐ๊ณผ ๊ฐ’ = 3

์ด๋Ÿฌํ•œ ๊ฒฐ๊ณผ๊ฐ€ ๋‚˜์˜ต๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ ์ค‘์š”ํ•œ ์ ์€ ์šฐ๋ฆฌ๊ฐ€ ์ง์ ‘์ ์ธ ๊ฐ’์„ ๋ฐฐ์—ด์—์„œ ์ถ”์ถœํ•  ๋•Œ์— ์—”์ง„์€ apple์ด ์ €์žฅ๋œ ๊ณณ์„ 1๋ฒˆ์ด ์•„๋‹Œ 0๋ฒˆ์œผ๋กœ ์•Œ๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค๋ฉด ์šฐ๋ฆฌ๊ฐ€ 'banana'๋ฅผ ์ฝ˜์†”์— ์ถœ๋ ฅํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

const fruits = [apple, banana, orange];

console.log(fruits[1]);

๊ฒฐ๊ณผ ๊ฐ’ = banana

์ฆ‰, fruits.length๋Š” ๋ฐฐ์—ด์˜ ๊ธธ์ด๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. ์ด 3๊ฐœ์˜ ๋ฐฐ์—ด์•ˆ์˜ ์›์†Œ๊ฐ€ ํฌํ•จ๋˜์–ด ์žˆ๊ธฐ ๋•Œ๋ฌธ์— 3์ด ๋ฐ˜ํ™˜ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ์ปดํ“จํ„ฐ์˜ ํ”„๋กœ์„ธ์Šค๋กœ ํ˜„์žฌ ๋ฐฐ์—ด์˜ ์ˆœ์„œ๋ฅผ ๋”ฐ์ ธ๋ณด๋ฉด [ 0 (apple), 1(banana), 2(orange) ]๋กœ ๊ธฐ์–ต๋˜๊ณ  ์žˆ๋Š” ๊ฒƒ ์ž…๋‹ˆ๋‹ค.

๐Ÿ™‹โ€โ™‚๏ธ์ด length ๋ฉ”์†Œ๋“œ๋ฅผ ์–ด๋–ป๊ฒŒ ํ™œ์šฉํ•  ์ˆ˜ ์žˆ์„๊นŒ?

์šฐ๋ฆฌ๋Š” ๊ณผ์ œ๋ฅผ ๋ฐ›์•˜์Šต๋‹ˆ๋‹ค.์–ด์ œ ํšŒ์›๊ฐ€์ž…ํ•œ ์ •๋ณด๋ฅผ ์ถœ๋ ฅํ•ด์•ผํ•˜๋Š”๋ฐ, ํ˜„์žฌ ๋ฐฑ์—”๋“œ๋กœ ๋ถ€ํ„ฐ ๋ฐ›์€ Api๋Š” ๋ฐฐ์—ด์•ˆ์— ์ €์žฅ๋œ object ํ˜•์‹์ด๋ฉฐ 5๋ฒˆ์งธ ์ด์ƒ์˜ ์ž๋ฃŒ๋“ค์€ ์˜ค๋Š˜ ์ €์žฅ๋œ ๋‚ด์šฉ์ด๋ฏ€๋กœ ๊ทธ ๋ถ€๋ถ„์„ ์ œ์™ธํ•˜๊ณ  ์ถœ๋ ฅ์„ ํ•ฉ๋‹ˆ๋‹ค.

//api ์˜ˆ์‹œ

const user = [
  {name:'jun', old:'24', phonenumber:'01000000000'},
  {name:'jin', old:'25', phonenumber:'01011111111'},
  {name:'lin', old:'30', phonenumber:'01022222222'},
  ...
  ]

์ด๋Ÿฌํ•œ Api๋ฅผ ๋ฐ›์•˜๋‹ค๊ณ  ๊ฐ€์ •์„ ํ•ฉ๋‹ˆ๋‹ค. ... ์ดํ›„๋กœ๋Š” ๊ณ„์† ๋‹ค๋ฅธ ๋‚ด์šฉ์˜ object๋“ค์ด ์ €์žฅ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ ‡๋‹ค๋ฉด ์šฐ๋ฆฌ๋Š” ์ด๋Ÿฌํ•œ ์ฝ”๋“œ๋กœ 5๋ฒˆ์งธ ๊นŒ์ง€์˜ object๋“ค์„ ์ถ”์ถœํ•  ์ˆ˜ ์žˆ์„ ๊ฒƒ ์ž…๋‹ˆ๋‹ค.

if (user.length > 5) {
  user.length = 5;
  }

console.log(user);

๊ทธ๋ฆฌ๊ณ  ๊ทธ ์ค‘ 2๋ฒˆ์งธ ์œ ์ €์˜ ์ •๋ณด๋ฅผ ๋ถˆ๋Ÿฌ์˜ค๊ณ  ์‹ถ๋‹ค๋ฉด

console.log(user[1])

์ด๋ ‡๊ฒŒ ํ•˜๋ฉด ๋ฐฐ์—ด ์ค‘ 5๋ฒˆ ์ด์ „์˜ ์œ ์ €๋“ค์˜ ์ •๋ณด์™€ ๊ทธ ์ค‘ 2๋ฒˆ์งธ ์ˆœ์„œ์— ํ•ด๋‹น๋˜๋Š” ์œ ์ €์˜ ์ •๋ณด๋ฅผ ๊ฐ€์ ธ์˜ฌ ์ˆ˜ ์žˆ์„ ๊ฒƒ ์ž…๋‹ˆ๋‹ค.

๐Ÿ’ก2. isArray

isArray๋Š” ๊ฐ์ฒด๊ฐ€ ๋ฐฐ์—ด์ธ์ง€ ์•„๋‹Œ์ง€๋ฅผ booleanํ˜•์‹์œผ๋กœ ๋ฐ˜ํ™˜ํ•ด ์ค๋‹ˆ๋‹ค.
์˜ˆ๋ฅผ ๋“ค๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

const numbers = [1,2,3,4,5]

const people = {'jun','jin','people}
                
console.log(Array.isArray(numbers))
console.log(Array.isArray(people))

๊ฒฐ๊ณผ ๊ฐ’ : true, false

์ด๋Ÿฐ ์‹์œผ๋กœ ํŠน์ • object๊ฐ€ ๋ฐฐ์—ด์ธ์ง€ ์•„๋‹Œ์ง€ ํ™•์ธํ•ด์ฃผ๋Š” ์ฝœ๋ฐฑํ•จ์ˆ˜ ์ž…๋‹ˆ๋‹ค.

๐Ÿ’ก3. concat

concat์€ ๋‘ ๋ฐฐ์—ด์„ ํ•˜๋‚˜๋กœ ํ•ฉ์ณ์ฃผ๋Š” ๋‚ด์žฅํ•จ์ˆ˜ ์ž…๋‹ˆ๋‹ค. ๋ฐฐ์—ด1๊ณผ ๋ฐฐ์—ด2๋ฅผ concatํ•˜๋ฉด ๋‘˜์ด ํ•ฉ์ณ์ง„ ๋ฐฐ์—ด์ด ๋˜๋Š” ๊ฒƒ ์ž…๋‹ˆ๋‹ค.

์„ ์–ธ ๋ฐฉ์‹์€ Arr1.concat(Arr2) ์ด๋Ÿฐ ์‹์œผ๋กœ ๋ฐฐ์—ด1์˜ ๋’ค์— concatํ•จ์ˆ˜๋ฅผ ๋ถˆ๋Ÿฌ์™€ ์ธ์ž๋กœ Arr2๋ฅผ ๋’ค๋กœ ๋ถ™ํžˆ๋Š” ํ˜•์‹์ž…๋‹ˆ๋‹ค. ๋‹ค์–‘ํ•˜๊ฒŒ ํ™œ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
๋ฐฐ์—ด๋กœ ๋งŒ๋“ค์–ด์ง„ ๊ฐ์ฒด๊ฐ€ ์•„๋‹Œ ๊ฒƒ์ด ๋“ค์–ด์™€๋„ ์ž๋™์œผ๋กœ ๊ทธ ๊ฐ์ฒด์˜ ์ธ์ž๋กœ์จ ๋“ค์–ด๊ฐ€๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.

์ฝ”๋“œ๋กœ ์˜ˆ๋ฅผ ๋“ค์–ด ๋ด…๋‹ˆ๋‹ค.

const number = [1, 2, 3];
const people = ['์ฒ ์ˆ˜', '์˜ํฌ', 'ํ›ˆ์ด']

console.log(number.concat(people))

๊ฒฐ๊ณผ ๊ฐ’: [1, 2, 3,'์ฒ ์ˆ˜','์˜ํฌ','ํ›ˆ์ด']

concat์€ ์–•์€ ๋ณต์‚ฌ์ž…๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ number.concat(people) ๋ฅผ ํ•˜์—ฌ๋„ ์›๋ณธ์€ ๋ณ€ํ•จ์ด ์—†์Šต๋‹ˆ๋‹ค.

๐Ÿ’ก4. fill

๋ฐฐ์—ด ์•ˆ์˜ ์›์†Œ๋ฅผ ํ•˜๋‚˜์˜ ์ •์ ์ธ ๊ฐ’์œผ๋กœ ์ฑ„์›๋‹ˆ๋‹ค. ์ฒซ ๋ฒˆ์งธ ๋งค๊ฐœ๋ณ€์ˆ˜๋Š” ์ฑ„์šธ ๊ฐ’, ๊ทธ ๋‹ค์Œ์€ ์–ด๋””์„œ ๋ถ€ํ„ฐ ์ฑ„์šธ ๊ฒƒ์ธ์ง€ ๊ทธ ๋‹ค์Œ์€ ๋งˆ๋ฌด๋ฆฌ๋ฅผ ์–ด๋””์„œ ํ•  ๊ฒƒ์ธ์ง€๋ฅผ ์ •ํ•ฉ๋‹ˆ๋‹ค.

const numbers = [1, 2, 3, 4]

console.log(numbers.fill(5,1)
console.log(numbers.fill(5,2,7)

๊ฒฐ๊ณผ ๊ฐ’: [ 1, 5, 5, 5 ], [ 1, 2, 5 ,5]

๋งŒ์•ฝ ๋งˆ๋ฌด๋ฆฌ ๋งค๊ฐœ๋ณ€์ˆ˜๊ฐ€ ์„ ์–ธ๋œ ๋ฐฐ์—ด์˜ ํฌ๊ธฐ๋ณด๋‹ค ํฌ๋‹ค๋ฉด ์›๋ž˜ ์„ ์–ธ๋œ ๋ฐฐ์—ด์œผ ํฌ๊ธฐ๊นŒ์ง€๋งŒ ์ถœ๋ ฅ๋ฉ๋‹ˆ๋‹ค.

๐Ÿ’ก5. fillter

filter๋Š” ๋ณดํ†ต ์‚ฌ์ดํŠธ์— ํ”ํžˆ ์žˆ๋Š” '๊ฒ€์ƒ‰'๋“ฑ์˜ ๊ธฐ๋Šฅ์— ํ™œ์šฉ๋ฉ๋‹ˆ๋‹ค. filter๋‚ด์žฅํ•จ์ˆ˜๋Š” ๋ง ๊ทธ๋Œ€๋กœ ๋ฐฐ์—ด์—์„œ ํŠน์ • ๊ฐ’์„ ๊ฑธ๋Ÿฌ๋‚ด๋Š” ํ•จ์ˆ˜์ž…๋‹ˆ๋‹ค.

๊ฐ ์š”์†Œ๋ฅผ ์‹œํ—˜ํ•  ํ•จ์ˆ˜๋ฅผ ์ฝœ๋ฐฑํ•จ์ˆ˜๋กœ ๋ฐ›๊ณ , ๊ทธ ํ•จ์ˆ˜์— true๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ๊ฐ’๋“ค์„ ๋‹ด๊ณ  false๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ๊ฐ’๋“ค์„ ๊ฑธ๋Ÿฌ๋ƒ…๋‹ˆ๋‹ค.

๋‹ค์Œ ์ฝ”๋“œ๋กœ ์‚ดํŽด๋ด…๋‹ˆ๋‹ค.

const words = ['hello', 'bye', 'people', 'monkey', 'computer']

const lowWords = words.filter(word => word.length < 5);

console.log(lowWords);

๊ฒฐ๊ณผ ๊ฐ’: ["bye"]

๋ฐฐ์—ด ๋‚ด์—์„œ ์ฝœ๋ฐฑํ•จ์ˆ˜๋Š” ์š”์†Œ word๋ฅผ ์ธ์ž๋กœ ๋ฐ›๊ณ  word์˜ ๊ธธ์ด๋Š” 5๋ณด๋‹ค ์ž‘์Šต๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ true๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ์š”์†Œ๋Š” bye๋ฐ–์— ์—†๊ธฐ ๋•Œ๋ฌธ์— bye๋งŒ ์ฝ˜์†”์— ์ถœ๋ ฅ๋˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.

๋‹ค์Œ์œผ๋กœ๋Š” ํŠน์ • ๋ฌธ์ž์—ด์„ ํฌํ•จํ•˜๋Š” ๊ฐ’๋งŒ ๋ฐ˜ํ™˜ํ•˜๋Š” '๊ฒ€์ƒ‰'ํ•จ์ˆ˜๋ฅผ ์ž‘์„ฑํ•ด ๋ด…๋‹ˆ๋‹ค.

const fruits = ['apple', 'banana', 'grapes', 'mango', 'orange'];

//๊ฒ€์ƒ‰ ์กฐ๊ฑด์— ๋”ฐ๋ฅธ ํ•„ํ„ฐ๋ง
const filterItems = (query) => {
  return fruits.filter((el) =>
    el.toLowerCase().indexOf(query.toLowerCase()) > -1
  );
}

console.log(filterItems('ap')); 
console.log(filterItems('an')); 

๊ฒฐ๊ณผ ๊ฐ’1 : ['apple', grapes']
๊ฒฐ๊ณผ ๊ฐ’2: ['mango', 'banana']

์ด๋ ‡๊ฒŒ ํ•„ํ„ฐํ•จ์ˆ˜๋Š” ํŠน์ • ๋ฐฐ์—ด์—์„œ ๊ฑธ๋Ÿฌ๋‚ด๊ณ ์‹ถ์€ ์ž‘์—…์„ ํ•  ๋•Œ ์‚ฌ์šฉ์ด ๋ฉ๋‹ˆ๋‹ค.

๋‹ค์–‘ํ•œ ๋ฐฐ์—ด๋‚ด์žฅํ•จ์ˆ˜๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ ๋ฐฐ์—ด๋‚ด์žฅํ•จ์ˆ˜๋Š” ์“ธ ์ผ์ด ๋งŽ์„ ๊ฒƒ ๊ฐ™๊ณ  ์‹ค์ œ๋กœ ๋งŽ์ด ์‚ฌ์šฉ๋˜๊ณ  ์žˆ๋‹ค๊ณ  ํ•˜๋‹ˆ ๋ณต์Šตํ•˜๋ฉด์„œ ์ •๋ฆฌ๋ฅผ ํ•˜๋ฉด ๋ถ„๋ช… ์“ธ ์ผ์ด ๋งŽ์„ ๊ฒƒ์ด๋ผ๊ณ  ์ƒ๊ฐํ•ฉ๋‹ˆ๋‹ค. ๋ชจ๋“  ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ๋ฐฐ์—ด๋‚ด์žฅํ•จ์ˆ˜๋ฅผ ์‹œ๋ฆฌ์ฆˆ๋กœ ๊ณ„์† ์—ฐ์žฌํ•  ๊ณ„ํš์ž…๋‹ˆ๋‹ค.

๐Ÿ“ƒ ์ฐธ๊ณ ๋ฌธํ—Œ

Mozilla MDN

profile
์ข‹์€ Front-end ๊ฐœ๋ฐœ์ž๊ฐ€ ๋˜๊ธฐ ์œ„ํ•ด ๋…ธ๋ ฅํ•˜๋Š” ํ•™์ƒ์ž…๋‹ˆ๋‹ค.

0๊ฐœ์˜ ๋Œ“๊ธ€