๐Ÿ—ƒ Javascript array

Dayยท2022๋…„ 4์›” 4์ผ
0
post-thumbnail

๐Ÿ“ฆ๋ฐฐ์—ด

์š”์†Œ ์ˆ˜์ •

let myArray = [ 19, 44, 'good', 9 ]
console.log(myArray)                      [19 ,44 , 'good' , 9]
myArray[2] = 'bad'
console.log(myArray)                      [19 ,44 , 'bad' , 9]

์š”์†Œ ์ถ”๊ฐ€

push : ๋’ค์— ์š”์†Œ ์ถ”๊ฐ€
my Array.push('kiwi')
console.log(myArray)                       [19 ,44 , 'bad' , 9, 'kiwi]

์š”์†Œ ์‚ญ์ œ

pop : ๋์—์„œ ๋ถ€ํ„ฐ ํ•˜๋‚˜์”ฉ ์‚ญ์ œ
myArray.pop()
shift : ์•ž๋ถ€ํ„ฐ ํ•˜๋‚˜์”ฉ ์‚ญ์ œ

๋ฐฐ์—ด ์•ˆ์˜ ๋ฐฐ์—ด(์ค‘์ฒฉ๋œ ๋ฐฐ์—ด)

let myArray = [19, 44,'good',[100,200,500],false]
console.log(myArray[3])           [100, 200, 500] 
console.log(myArray[3][0])          100

slice()

slice ๋ฉ”์†Œ๋“œ๋Š” ๋ฐฐ์—ด ๋‚ด์˜ ํŠน์ •ํ•œ ์š”์†Œ์˜ index ๋ฒ”์œ„์— ๋”ฐ๋ผ ์ƒˆ๋กœ์šด ๋ฐฐ์—ด์„ ๋ฆฌํ„ดํ•œ๋‹ค.
splice ๋ฉ”์†Œ๋“œ์™€๋Š” ๋‹ค๋ฅด๊ฒŒ slice ๋ฉ”์„œ๋“œ๋Š” ์›๋ณธ ๋ฐฐ์—ด์„ ๋ณ€ํ˜•์‹œํ‚ค์ง€ ์•Š๋Š”๋‹ค.
๊ทธ๋ ‡๊ธฐ ๋•Œ๋ฌธ์— ์ด ๋ฉ”์†Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ๋Š” slice ๋ฉ”์„œ๋“œ๋ฅผ ์ ์šฉํ•œ ์ƒˆ๋กœ์šด ๋ณ€์ˆ˜๋ฅผ ์„ ์–ธํ•ด์ฃผ์–ด์•ผ ํ•œ๋‹ค.

์œ„์˜ ๊ทธ๋ฆผ์—์„œ๋Š” ์ธ์ž 2๊ฐœ๊ฐ€ ๋“ค์–ด๊ฐ”์ง€๋งŒ, slice ๋ฉ”์„œ๋“œ๋Š” ํ•„์š”์— ๋”ฐ๋ผ ์ธ์ž๋ฅผ ์ตœ์†Œ 1๊ฐœ๋งŒ ์“ธ ์ˆ˜๋„ ์žˆ๋‹ค.

  • ์ฒซ๋ฒˆ์งธ ์ธ์ž : ย ย ๋ฐฐ์—ด์˜ index์˜ ์‹œ์ž‘์ 
  • ๋‘๋ฒˆ์งธ ์ธ์ž :ย ย ย ๋ฐฐ์—ด์˜ index์˜ ๋์ 
    start๋ฒˆ์งธ ๋ถ€ํ„ฐ ์ „๋ถ€ ๋‹ค ~ end๋ฒˆ์งธ ์ „๊นŒ์ง€(end๋ฒˆ์งธ ๊บผ๋Š” ๋นผ๊ณ )
let nums = [1,2,3,4,5]
let nums_new = nums.slice(1,4)

console.log(nums) // [ 1, 2, 3, 4, 5 ]
console.log(nums_new) // [ 2, 3, 4 ]

nums ๋ฐฐ์—ด์—์„œ, 1๋ฒˆ์งธ๋ถ€ํ„ฐ ์ „๋ถ€ ๋‹ค ~ 4๋ฒˆ์งธ ์ „๊นŒ์ง€(4๋ฒˆ์งธ ๊บผ๋Š” ๋นผ๊ณ )

์ธ์ž์— ์Œ์ˆ˜๊ฐ€ ๋“ค์–ด๊ฐ€๋Š” ๊ฒฝ์šฐ

let nums = [1,2,3,4,5]
let nums_new = nums.slice(-2)
โ€‹
console.log(nums) // [ 1, 2, 3, 4, 5 ]
console.log(nums_new) // [ 4, 5 ]

์Œ์ˆ˜๊ฐ€ ๋“ค์–ด๊ฐˆ ๊ฒฝ์šฐ ๋์—์„œ๋ถ€ํ„ฐ ํ•ด๋‹นํ•˜๋Š” ์ˆซ์ž ๋งŒํผ์˜ ์š”์†Œ๋ฅผ ๋‹ด์•„ ๋ฆฌํ„ด(-2๋ฉด ๋์—์„œ 2๊ฐœ)

(start ,end) ์—์„œ,
๋ถ€ํ˜ธ๊ฐ€ ๊ฐ™์„ ๋•Œ, start๊ฐ’์€ end๋ณด๋‹ค ์ž‘์•„์•ผ ํ•˜๊ณ ,
๋ถ€ํ˜ธ๊ฐ€ ๋‹ค๋ฅผ ๋•Œ๋Š” start๊ฐ’์ด ์–‘์ˆ˜์—ฌ์•ผ ๋ง์ด ๋จ.

Array ๊ฐ์˜ฅ์— ๊ฐ‡ํžŒ 2 ๊บผ๋‚ด๊ธฐ

๋ฌธ์ œ)
let prisoners = [[0, 1],[1,2],[0,0]];
saveNumberTwo(prisoners) // 2
// slice ๋ฉ”์„œ๋“œ์˜ ๊ด„ํ˜ธ ์•ˆ์— ์Œ์ˆ˜๋งŒ ๋„ฃ๊ธฐ
 // ๋ณ€์ˆ˜ answer์— ํŠน์ •ํ•œ ๊ฐ’์„ ๋Œ€์ž…ํ•˜๊ธฐ
let prisoners = [[0,1],[1,2],[0,0]];

function saveNumberTwo() {

 let temp = prisoners.slice(-2,-1);
 let answer;
answer = temp[0][1];
 return answer;
}

saveNumberTwo(prisoners) ;

ํ•ด๋ณด๋‹ˆ๊นŒ, prisoners ๋ผ๋Š” ๋ฐฐ์—ด์„ ๋งŒ๋“ค ๋•Œ, ํ•จ์ˆ˜ ์•ˆ์—์„œ ์„ ์–ธํ•˜๋ฉด ์•ˆ๋˜๋Š” ๊ฒŒ ํ—ท๊ฐˆ๋ ธ๋‹ค.

splice()

splice ๋ฉ”์†Œ๋“œ๋Š” ๋ฐฐ์—ด ๋‚ด์˜ ํŠน์ •ํ•œ ์š”์†Œ๋ฅผ ์‚ญ์ œํ•˜๊ฑฐ๋‚˜,๋‹ค๋ฅธ ์š”์†Œ๋กœ ๋Œ€์น˜ํ•˜๊ฑฐ๋‚˜ ์ƒˆ๋กœ์šด ์š”์†Œ๋ฅผ ์ถ”๊ฐ€ํ•  ๋•Œ ์‚ฌ์šฉํ•œ๋‹ค.
splice ๋ฉ”์„œ๋“œ๋ฅผ ์“ธ ๋•Œ๋Š” ์ธ์ž์˜ ์ˆœ์„œ์— ์ฃผ์˜ํ•ด์•ผ ํ•œ๋‹ค.

์œ„์˜ ๊ทธ๋ฆผ์—์„œ๋Š” ์ธ์ž 3๊ฐœ๊ฐ€ ๋“ค์–ด๊ฐ”์ง€๋งŒ, splice ๋ฉ”์„œ๋“œ๋Š” ํ•„์š”์— ๋”ฐ๋ผ ์ธ์ž๋ฅผ ์ตœ์†Œ 1๊ฐœ๋งŒ ์“ธ ์ˆ˜๋„ ์žˆ๋‹ค.

  • ์ฒซ๋ฒˆ์งธ ์ธ์ž : ๋ฐฐ์—ด์˜ index์˜ ์‹œ์ž‘์ (ํ•„์ˆ˜), ์Œ์ˆ˜๊ฐ€ ๋  ๊ฒฝ์šฐ ๋ฐฐ์—ด ๋์—์„œ๋ถ€ํ„ฐ ๊ธธ์ด๋ฅผ ๋‚˜ํƒ€๋‚ธ๋‹ค.
  • ๋‘๋ฒˆ์งธ ์ธ์ž : ์‚ญ์ œํ•  ์š”์†Œ์˜ ๊ฐœ์ˆ˜
  • ์„ธ๋ฒˆ์งธ ์ธ์ž ์ดํ›„ : ์ถ”๊ฐ€ํ•˜๊ณ  ์‹ถ์€ ์š”์†Œ

ex)

[1,2,3,4,5] ๋ผ๋Š” ์—ด์—์„œ ์ˆซ์ž 3์„ ์ œ๊ฑฐํ•˜๊ณ  ๊ทธ ์ž๋ฆฌ์— 10์„ ์ถ”๊ฐ€ํ•ด์ฃผ์„ธ์š”.

let num = [1,2,3,4,5];
num.splice(2,1,10);

console.log(num); // [ 1, 2, 10, 4, 5 ]

๋ณดํ†ต ๋Œ“๊ธ€ ์‚ญ์ œ ๊ธฐ๋Šฅ ๊ตฌํ˜„ํ•  ๋•Œ ๋งŽ์ด ์‚ฌ์šฉํ•œ๋‹ค.

๋‹ค์ฐจ์› ๋ฐฐ์—ด์— for๋ฌธ์„ ํ™œ์šฉํ•ด์„œ ์“ฐ๋Š” ์˜ˆ์ œ

for๋ฌธ์˜ ์ค‘๊ด„ํ˜ธ ์•ˆ์—๋Š” ๋˜ ๋‹ค๋ฅธ for๋ฌธ์„ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

10 ์ด์ƒ์˜ ์ˆซ์ž๋งŒ ๋ฐฐ์—ด์— ๋‹ด์•„ ๋ฆฌํ„ดํ•˜๋Š” ํ•จ์ˆ˜

let numBox = [[1,2,15,3],[4,5,6,10],[31,7,8,9,20]];
โ€‹
function extractOverTen(list) {
  let temp = [];
  for(let i=0; i < list.length; i++) {
    for(let j=0; j < list[i].length; j++) {
      if(list[i][j] >= 10) {
        temp.push(list[i][j]);
      }  
    }
  }
  return temp;
}
โ€‹
extractOverTen(numBox) // [ 15, 10, 31, 20 ]


i๊ฐ€ 0,1,2๋กœ ์›€์ง์ผ๋•Œ, j๊ฐ€ ๊ฐ๊ฐ์˜ ๋ฐ˜๋ณต๋ฌธ ์•ˆ์—์„œ 0๋ถ€ํ„ฐ ์›€์ง์ธ๋‹ค. ๋ชจ๋“  ์š”์†Œ๋“ค์˜ ๋ฒˆํ˜ธ๋ฅผ ๋งค๊ฒจ์„œ ์ฒ˜๋ฆฌํ•˜๋Š” ๋ฐฉ๋ฒ•

ex)

๋ฐ”๊ตฌ๋‹ˆ์—์„œ ๊ณฐํŒก์ด๋ฅผ ์ œ๊ฑฐํ•˜๋Š” ํ•จ์ˆ˜๋ฅผ ์ž‘์„ฑํ•ด์ฃผ์„ธ์š”.

let basket = [['์–‘ํŒŒ','๊ณฐํŒก์ด'],['๊ณฐํŒก์ด','๋นต','๋”ธ๊ธฐ์žผ'],['๊ทค','๊ณฐํŒก์ด','์‚ฌ๊ณผ']];
let basket = [['์–‘ํŒŒ','๊ณฐํŒก์ด'],['๊ณฐํŒก์ด','๋นต','๋”ธ๊ธฐ์žผ'],['๊ทค','๊ณฐํŒก์ด','์‚ฌ๊ณผ']];
function removeGerm(arr) {
  // ์—ฌ๊ธฐ์— ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•ด์ฃผ์„ธ์š”!
for(let i = 0; i< arr.length; i++){
for(let j = 0; j<arr[i].length; j++){
  if(arr[i][j] === '๊ณฐํŒก์ด'){
   arr[i].splice(j,1)
  }    
}
}
  return arr
}
console.log(removeGerm(basket)); ///
>
[ [ '์–‘ํŒŒ' ], [ '๋นต', '๋”ธ๊ธฐ์žผ' ], [ '๊ทค', '์‚ฌ๊ณผ' ] ];


## includes()

๋ฌธ์ž์—ด์ด ํŠน์ • ๋ฌธ์ž์—ด์„ ํฌํ•จํ•˜๋Š”์ง€ ํ™•์ธ ํ•˜๋Š” ๋ฉ”์†Œ๋“œ

`string.includes(searchString , length)`

- searchString :  ๊ฒ€์ƒ‰ํ•  ๋ฌธ์ž์—ด(ํ•„์ˆ˜ ์š”์†Œ), ๋Œ€์†Œ๋ฌธ์ž ๊ตฌ๋ถ„
- length :  ๊ฒ€์ƒ‰์„ `์‹œ์ž‘`ํ•  ์œ„์น˜(์„ ํƒ ์š”์†Œ), ๊ฐ’์ด ์—†์œผ๋ฉด ์ „์ฒด ๋ฌธ์ž์—ด ๋Œ€์ƒ
```java
ex)
'abzcd'.includes('z')     true
'abzcd'.includes('z',2)   true    
'abzcd'.includes('z',3)   false    cd๊ฐ€ z๋ฅผ ํฌํ•จํ•˜๋Š”์ง€ ๊ฒ€์‚ฌ
'abZcd'.includes('z',3)   false

concat()

concat ๋ฉ”์†Œ๋“œ๋Š” ์ฃผ์–ด์ง„ ๋ฐฐ์—ด์— ๊ธฐ์กด ๋ฐฐ์—ด์„ ํ•ฉ์ณ์„œ ์ƒˆ๋กœ์šด ๋ฐฐ์—ด์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค
์›๋ณธ ๋ฐฐ์—ด์€ ๋ณ€ํ•˜์ง€ ์•Š์œผ๋ฉฐ ์ƒˆ๋กœ์šด ๋ฐฐ์—ด์ด๋‚˜ ์›๋ณธ ๋ฐฐ์—ด์„ ์ˆ˜์ •ํ•ด๋„ ์„œ๋กœ ์˜ํ–ฅ์„ ๋ฐ›์ง€ ์•Š๋Š”๋‹ค.

let alphabet = ['a', 'b', 'c'];
let hangeul = ['ใ„ฑ', 'ใ„ด', 'ใ„ท'];

alphabet.concat(hangeul);      // [ 'a', 'b', 'c', 'ใ„ฑ', 'ใ„ด', 'ใ„ท' ]

๋ฐฐ์—ด์„ ๋ณ€์ˆ˜์— ์ง€์ •ํ•ด์„œ ์ธ์ž๋ฅผ ๋„˜๊ฒจ์ค€๋‹ค.


const alpha = ['a', 'b', 'c'];

1.
const arr = [1, [2, 3]];     
alpha.concat(arr);             // [ 'a', 'b', 'c', 1, [ 2, 3 ] ]

2.
alpha.concat(arr);
alpha.concat(1, [2, 3]);       // [ 'a', 'b', 'c', 1, 2, 3 ]

3.
const numbers = [1, 2, 3];
const numbers2 = [3, 4, 5];

numbers.concat(numbers2);      // [ 1, 2, 3, 3, 4, 5 ]

1์˜ ๊ฒฝ์šฐ๋Š”, ๋ฐฐ์—ด์„ ๋ณ€์ˆ˜์— ์ง€์ •ํ•ด์„œ ๋„˜๊ฒจ์คฌ๊ธฐ ๋•Œ๋ฌธ์— ๊ทธ๋Œ€๋กœ ๋„˜์–ด๊ฐ„๋‹ค.
2์˜ ๊ฒฝ์šฐ๋Š”, ํŒŒ๋ผ๋ฏธํ„ฐ(์ธ์ž)์—์„œ ๋ฐ”๋กœ ๋ฐฐ์—ด์„ ์ž‘์„ฑํ–ˆ๋Š”๋ฐ, ์ด๋Ÿฐ ๊ฒฝ์šฐ ๋ฐฐ์—ด์•ˆ์˜ ์›์†Œ๋“ค์„ ๋‹ค ๊บผ๋‚ด์„œ ํฌํ•จ์‹œํ‚ค๊ฒŒ ๋œ๋‹ค.
3์˜ ๊ฒฝ์šฐ์ฒ˜๋Ÿผ ์ค‘๋ณต์ด ์žˆ์–ด๋„ ์ƒ๊ด€์—†์ด ๋‹ค ๋‚˜์˜จ๋‹ค.

ex) num ๋ฐฐ์—ด์•ˆ์˜ ์š”์†Œ๋“ค์„ concat()์„ ์ด์šฉํ•ด์„œ ํ•˜๋‚˜์˜ ๋ฐฐ์—ด๋กœ ํ•ฉ์ณ์ง€๊ฒŒ ํ•ด์ฃผ์„ธ์š”.
์•„๋ž˜์™€ ๊ฐ™์€ ๊ฒฐ๊ณผ๊ฐ€ ๋‚˜์™€์•ผ ํ•ฉ๋‹ˆ๋‹ค. (ํžŒํŠธ: array์—์„œ index๋กœ ์ ‘๊ทผ)

[ 11, 12, 13, 14, 15, 16, 17, 18, 19 ]

let num = [[11, 12, 13], [14, 15, 16], [17, 18, 19]]; 


function makeNewArr (a) {

return a[0].concat(a[1].concat(a[2]))

}
console.log(makeNewArr(num));

์ค‘๋ณต์ œ๊ฑฐ ํ•˜๋Š” ๋ฒ•

[1, 2, 3, 4, 5, 3, 4, 5, 6, 7]

let eraseDuplicates = result.filter((el,index)=> 
result.indexOf(el)===index);

console.log(eraseDuplicates);      

[1, 2, 3, 4, 5, 6, 7]
๋ฐฐ์—ด์—์„œ ๊ฒ€์‚ฌํ•˜๊ณ  ์žˆ๋Š” ๊ฐ’(element)์ด ์ฒ˜์Œ ๋“ฑ์žฅํ•˜๋Š” index ๊ฐ’๊ณผ
๊ทธ ๊ฐ’์˜ index ๊ฐ’ ๋น„๊ต. ๋˜ ๋‚˜์˜จ ๊ฐ™์€ ๊ฐ’์€ ์ €๊ฒŒ ๋‹ค๋ฅด๋‹ˆ๊นŒ ํ•„ํ„ฐ๋กœ ๊ฑธ๋Ÿฌ์ง

ex)
ํŒŒ์Šคํƒ€์™€ ํ”ผ์ž์˜ ์žฌ๋ฃŒ๊ฐ€ ๋ฐฐ์—ด๋กœ ๋‚˜ํƒ€๋‚˜์žˆ์Šต๋‹ˆ๋‹ค. ์œ„์˜ ์ฝ”๋“œ๋ฅผ ์ฐธ๊ณ ํ•ด์„œ ์ค‘๋ณต๋œ ์žฌ๋ฃŒ๋ฅผ ๋บ€ ์ „์ฒด ์žฌ๋ฃŒ์˜ ๋ฐฐ์—ด ํ•œ ๊ฐœ๋ฅผ๋งŒ๋“ค์–ด์ฃผ์„ธ์š”.

์•„๋ž˜์™€ ๊ฐ™์€ ๊ฒฐ๊ณผ๊ฐ€ ๋‚˜์™€์•ผ ํ•ฉ๋‹ˆ๋‹ค.

์ค‘๋ณต๋œ ์žฌ๋ฃŒ๋ฅผ ๋บ€ ์ „์ฒด ์žฌ๋ฃŒ
[ 'tomato', 'basil', 'onion', 'chicken', 'cheese', 'olive', 'beef' ]

let pasta = ['tomato', 'basil', 'onion','chicken'];
let pizza = ['tomato', 'cheese', 'onion','olive','beef'];

function totalIngredients (a) {
 return a.filter((element,index)=>a.indexOf(element)===index;
 }
console.log(totalIngredients(pasta.concat(pizza)));

indexOf()

arr.indexOf(searchElement[,fromIndex])

๋ฐฐ์—ด ์•ˆ์—์„œ ์ฐพ์œผ๋ ค๋Š” ๊ฐ’(searchElement)์™€ ์ •ํ™•ํžˆ ์ผ์น˜(===)ํ•˜๋Š” ์ฒซ๋ฒˆ์งธ element์˜ index๋ฅผ ๋ฆฌํ„ดํ•œ๋‹ค. ย 

  • fromIndex ์ž…๋ ฅ ์•ˆํ•˜๋ฉด 0๋ถ€ํ„ฐ ๊ฒ€์ƒ‰ํ•œ๋‹ค.
  • ์ฐพ์œผ๋ ค๋Š” ๊ฐ’์ด ๋ฐฐ์—ด์— ์—†์œผ๋ฉด -1์„ ๋ฆฌํ„ดํ•œ๋‹ค.
['a','b','a','b']


arr.indexOf('a',1); // 2

์˜คํƒ€ ํ”„๋กœ๋ž˜๋ฐ์„ ํ”„๋กœ๊ทธ๋ž˜๋ฐ์œผ๋กœ ๋ฐ”๊พธ์–ด ์ฃผ๋Š” ์ฝ”๋“œ


let info = "JavaScript๋Š” ํ”„๋กœ๋ž˜๋ฐ ์–ธ์–ด์ด๋‹ค.";
let firstChar = info.indexOf("ํ”„๋กœ๋ž˜๋ฐ"); 

console.log(info, firstChar);

if (firstChar !== -1) { 
 info = info.slice(0, firstChar) + "ํ”„๋กœ๊ทธ๋ž˜๋ฐ" + info.slice(firstChar+4, info.length); 
}

console.log(info);

a๋ฅผ ๋ฐฐ์—ด 1๋ฒˆ์งธ๋ถ€ํ„ฐ ์ฐพ์œผ๋ฉด ์–ด๋””์— ์žˆ๋‚˜?

new Array()

๋ฐฐ์—ด์„ ๋งŒ๋“œ๋Š” ๋ฐฉ๋ฒ•์€ ๊ฐ„๋‹จํžˆ []๋ฅผ ์“ฐ๋Š” ๋ฐฉ๋ฒ•๋„ ์žˆ์ง€๋งŒ, new Array() ๋ฉ”์†Œ๋“œ๋ฅผ ์“ฐ๋Š” ๋ฐฉ๋ฒ•๋„ ์žˆ๋‹ค.

 console.log(new Array(4))

n๊ฐœ์˜ ๊ณต๊ฐ„์„ ๊ฐ€์ง„ ๋นˆ ๋ฐฐ์—ด์„ ๋งŒ๋“ค๊ธฐ ์œ„ํ•ด์„œ ์šฐ๋ฆฌ๋Š” new Array(n) ์ด๋ผ๋Š” ๋ฉ”์†Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค. ์ด๋ ‡๊ฒŒ ๋งŒ๋“ค์–ด์ง„ ๋ฐฐ์—ด์€ ๋นˆ ๋ฐฐ์—ด์ด๊ณ  ์•ˆ์— ์š”์†Œ๋ฅผ ํฌํ•จํ•˜๊ณ  ์žˆ์ง€ ์•Š์ง€๋งŒ, ๊ณต๊ฐ„์„ ๋งŒ๋“ค์–ด์ฃผ์—ˆ๊ธฐ ๋•Œ๋ฌธ์— arr.length๋ฅผ ์ฝ˜์†”์ฐฝ์— ์ฐ์–ด๋ณด๋ฉด 4๊ฐ€ ๋‚˜์˜ฌ ๊ฒƒ์ž…๋‹ˆ๋‹ค.

๋นˆ ๋ฐฐ์—ด์— ๊ฐ’์„ ํ• ๋‹นํ•˜๋ ค๋ฉด ๋ฐ˜๋ณต๋ฌธ์„ ์ด์šฉ

let arr = new Array(4);
for(let i=0; i< arr.length ; i++) {
  arr[i] = i+1
}
console.log(arr) // [ 1, 2, 3, 4 ]

push()

push() ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ *๋ฐฐ์—ด์˜ ๋์— ํ•˜๋‚˜ ์ด์ƒ์˜ ์š”์†Œ๋ฅผ ์ถ”๊ฐ€ํ•  ์ˆ˜ ์žˆ๋‹ค.

let arr = [1,2,3];
arr.push(4); 
// arr is now [1,2,3,4]

unshift()

push()๋ž‘ ๊ฐ™์€๋ฐ, ๋ฐฐ์—ด์˜ ๋งจ ์•ž๋ถ€๋ถ„์— ์ถ”๊ฐ€ ํ•œ๋‹ค.

pop()

pop() ๋ฉ”์†Œ๋“œ๋Š” ๋ฐฐ์—ด์˜ ๋งˆ์ง€๋ง‰ ์š”์†Œ๋ฅผ ์ œ๊ฑฐํ•˜๋ฉฐ ์ œ๊ฑฐ๋œ ์š”์†Œ๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

let threeArr = [1, 4, 6];
let oneDown = threeArr.pop();

console.log(oneDown); // Returns 6
console.log(threeArr); // Returns [1, 4]

shift()

shift() ๋ฉ”์†Œ๋“œ๋Š” ๋ฐฐ์—ด์˜ ์ฒซ๋ฒˆ์งธ ์š”์†Œ๋ฅผ ์ œ๊ฑฐํ•˜๋ฉฐ ์ œ๊ฑฐ๋œ ์š”์†Œ๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

pop() , shift() ๋‘˜๋‹ค. ์ฒ˜์Œ ๋ฐฐ์—ด์„ ๋ฐ”๊พธ๋Š” ๊ฑฐ๋ผ์„œ, ์ฒ˜์Œ ๋ฐฐ์—ด์„ ๋‹ค์‹œ ๋ถˆ๋Ÿฌ์˜ค๋ฉด ์š”์†Œ๊ฐ€ ์ œ๊ฑฐ๋˜์–ด ์žˆ๋‹ค.
๊ทธ๋ฆฌ๊ณ  .pop() ์ด๋‚˜ .shift() ์ƒํƒœ๋กœ ๊ทธ๋ƒฅ ๋ถˆ๋Ÿฌ์˜ค๋ฉด ์ œ๊ฑฐ๋œ ์š”์†Œ๊ฐ€ ๋ฐ˜ํ™˜๋จ.

join()

join() ๋ฉ”์„œ๋“œ๋Š” ๋ฐฐ์—ด์˜ ๋ชจ๋“  ์š”์†Œ๋ฅผ ์—ฐ๊ฒฐํ•ด ํ•˜๋‚˜์˜ ๋ฌธ์ž์—ด๋กœ ๋งŒ๋“ ๋‹ค.

separator๋Š” ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ, ๋ฐฐ์—ด์˜ ๊ฐ ์š”์†Œ๋ฅผ ๊ตฌ๋ถ„ํ•  ๋ฌธ์ž์—ด์ด๋‹ค.
์ด ๊ตฌ๋ถ„์ž๋Š” ํ•„์š”ํ•œ ๊ฒฝ์šฐ ๋ฌธ์ž์—ด๋กœ ๋ณ€ํ™˜๋˜๊ณ , ์ƒ๋žตํ•˜๋ฉด ๋ฐฐ์—ด์˜ ์›์†Œ๋“ค์˜ ์‰ผํ‘œ๋กœ ๊ตฌ๋ถ„๋œ๋‹ค.

const arr = ['๋ฐ”๋žŒ', '๋น„', '๋ฌผ'];

console.log(arr.join());
// ๋ฐ”๋žŒ,๋น„,๋ฌผ

console.log(arr.join(''));
// ๋ฐ”๋žŒ๋น„๋ฌผ

console.log(arr.join('-'));
// ๋ฐ”๋žŒ-๋น„-๋ฌผ

Array.from()

๋ฌธ์žํ˜•๋งŒ ๊ฐ€๋Šฅ.

Array.from('Tei');

 //["T", "e", "i"]
 
 Array.from('123');

 //["1", "2", "3"]

Array.sort()

sort๋Š” ์ •๋ ฌ ์ˆœ์„œ๋ฅผ ์ •์˜ํ•˜๋Š” ํ•จ์ˆ˜์ธ๋ฐ, ๊ทธ ์›๋ฆฌ๊ฐ€ a,b ๋‘๊ฐœ์˜ element๋ฅผ ์ž…๋ ฅ๋ฐ›์•„์„œ ์ € ์•ˆ์˜ compareFunction ์ด ๋ฆฌํ„ดํ•˜๋Š” ๊ฐ’์ด 0๋ณด๋‹ค ํฐ์ง€ ์ž‘์€์ง€์— ๋”ฐ๋ผ ์ˆœ์„œ๋ฅผ ๋ฐ”๊ฟ”์ฃผ๋Š” ํ•จ์ˆ˜

์ˆซ์ž๋ฅผ string ์œผ๋กœ ๋ด์„œ, ๊ทธ๋ƒฅ array.sort()ํ•˜๋ฉด ์ˆซ์ž๋ฅผ ์ œ๋Œ€๋กœ ์ •๋ ฌ ๋ชปํ•ด์ค€๋‹ค.

๊ฐ„๋‹จํ•˜๊ฒŒ, a,b ์žˆ์„ ๋•Œ. ํ•จ์ˆ˜ ๋ฆฌํ„ด๊ฐ’์ด 0๋ณด๋‹ค ํฌ๋ฉด b๊ฐ€ ์•ž ์ž‘์œผ๋ฉด a๊ฐ€ ์•ž 0์ผ๋• ๊ทธ๋Œ€๋กœ
๊ทธ๋ž˜์„œ ์šฐ๋ฆฌ๋Š” ์ˆซ์ž ๋น„๊ต ํ•˜๋ ค๊ณ ,
์—ฌ๊ธฐ์„œ์˜ ํ•จ์ˆ˜์ธ compareFunction์˜ ๋ฆฌํ„ด๊ฐ’์„ ๊ฐ•์ œ๋กœ ์ปค์Šคํ…€ํ•ด์„œ ๋งŒ๋“ค์–ด์ค˜์„œ ๊ทธ ๋ฆฌํ„ด๊ฐ’์„ 0๊ณผ ๋น„๊ตํ•˜๊ฒŒ ๋งŒ๋“ค์–ด ์˜ค๋ฆ„์ฐจ์ˆœ, ๋‚ด๋ฆผ์ฐจ์ˆœ์„ ์ •ํ•˜๋Š” ์›๋ฆฌ

Array callback method

callback ํ•จ์ˆ˜ : ์ธ์ž๋กœ ์ „๋‹ฌ๋˜๋Š” ํ•จ์ˆ˜๋ฅผ ๋งํ•จ

arrow function ์ด ๊ฐ€์žฅ ๋งŽ์ด ์‚ฌ์šฉ๋˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ์ด ํ•จ์ˆ˜ ์“ธ ๋•Œ ์ด๋‹ค.

arrow function์„ ์กฐ๊ธˆ ๋” ์—ฐ์Šตํ•˜๊ธฐ ์œ„ํ•ด array์˜ ๋ฐ˜๋ณต๋ฌธ์œผ๋กœ ์‚ฌ์šฉ๋˜๋Š” map, forEach ๋ฉ”์†Œ๋“œ๋ฅผ ํ•จ๊ป˜ ๋ณด์ž.

๐Ÿ’Ž Array.map()

map ๋ฉ”์„œ๋“œ๋Š” ๋ฐฐ์—ด์„ ๋ฐ˜๋ณตํ•ด์ค€๋‹ค.
callback ํ•จ์ˆ˜๋ฅผ ์‹คํ–‰ํ•˜๊ณ , returnํ•œ ๊ฐ’์œผ๋กœ ๋งค(each) ์š”์†Œ๋ฅผ ์ˆ˜์ •ํ•ด ์ค€๋‹ค.

map ๋ฉ”์†Œ๋“œ์˜ return ๊ฐ’์€ ์ˆ˜์ •๋œ ๊ฐ’์œผ๋กœ ๋‹ค์‹œ ์ƒ์„ฑ๋œ ๋ฐฐ์—ด์ด๋‹ค.


const arr = [0,1,2,3];

let squaredArr = arr.map(function(element){
    return element * element;
});
// ํ˜น์€ arrow ํ•จ์ˆ˜ ๊ฐ€๋Šฅ
let squaredArr = arr.map(element => element * element);

console.log(squaredArr); // [ 0, 1, 4, 9 ]

์ข€ ์–ด๋ ต๊ณ  ์ค‘์š”ํ•œ ์˜ˆ์ œ)

const arr = [0,1,2,3];

let squaredArr = arr.map(function(element, index, array){

    console.log(`${array}์˜ ${index}๋ฒˆ์งธ ์ธ์ž : ${element}`);
    
    return element * element; }
    );
    
    // arr.map์€, arr ๋ฐฐ์—ด ์š”์†Œ ํ•˜๋‚˜ํ•˜๋‚˜๋กœ map์•ˆ์— ์žˆ๋Š” ํ•จ์ˆ˜๋ฅผ ๋ˆ๋‹ค. ํ•จ์ˆ˜ ์•ˆ์˜ ๋‚ด์šฉ์€ ์‹คํ–‰์‹œํ‚ค๊ณ ,
    
   // ๋ฆฌํ„ด์œผ๋กœ ์“ฐ์ธ ๊ฐ’๋“ค์€ ํ•˜๋‚˜ํ•˜๋‚˜ ๋‹ค์‹œ ๋„์ง‘์–ด๋‚ด์„œ ์ƒˆ ๋ฐฐ์—ด์„ ๋งŒ๋“ฌ.
    
0,1,2,3์˜ 0๋ฒˆ์งธ ์ธ์ž : 0
0,1,2,3์˜ 1๋ฒˆ์งธ ์ธ์ž : 1
0,1,2,3์˜ 2๋ฒˆ์งธ ์ธ์ž : 2
0,1,2,3์˜ 3๋ฒˆ์งธ ์ธ์ž : 3

console.log(squaredArr); // [ 0, 1, 4, 9 ]

const moreThan100 = nums => {

  let a = nums.map(el => {

if(el >= 100){

  return true;
}else{

  return false;
}
    
  });
return a ; 
}
let nums = [100, 9, 30, 7];
  console.log(moreThan100(nums)); //  [true, false, false, false];
  
// morethan100 ์ด๋ผ๋Š” ํ•จ์ˆ˜๋Š”, ๋ฐฐ์—ด์„ ์ธ์ž๋กœ ๋ฐ›๋Š”๋‹ค.
 // ๊ทธ ๋ฐฐ์—ด์— map์„ ๊ฑธ์–ด์„œ, ๋ฆฌํ„ด์œผ๋กœ ์ƒˆ๋กœ๋งŒ๋“ค์–ด์ง„ ๋ฐฐ์—ด ๊ฐ’์„ a ๋ผ๊ณ  ํ•œ๋‹ค. 
// ์ด ํ•จ์ˆ˜๋Š” a๋ฅผ ๋ฆฌํ„ดํ•œ๋‹ค. 
  

split ์ด๋ž‘ map ํ™œ์šฉ ์ค‘์š”ํ•œ ์˜ˆ์ œ!!

const formatDate = dates => {
let a = dates.map(el => {


let spt = el.split('-');
  
  return `${spt[0]}๋…„ ${spt[1]}์›” ${spt[2]}์ผ`;
  
});
  return a;
}

let pp = ['2019-03-21', '2019-04-21', '2019-05-21'];
console.log(formatDate(pp));
//['2019๋…„ 03์›” 21์ผ', '2019๋…„ 04์›” 21์ผ', '2019๋…„ 05์›” 21์ผ']

Array.forEach()

forEach๋Š” for๋ฌธ๊ณผ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ๋ฐ˜๋ณต์ ์ธ ๊ธฐ๋Šฅ์„ ์ˆ˜ํ–‰ํ•  ๋•Œ ์‚ฌ์šฉํ•œ๋‹ค.

ํ•˜์ง€๋งŒ for๋ฌธ์ฒ˜๋Ÿผ index์™€ ์กฐ๊ฑด์‹, increase๋ฅผ ์ •์˜ํ•˜์ง€ ์•Š์•„๋„ callback ํ•จ์ˆ˜๋ฅผ ํ†ตํ•ด ๊ธฐ๋Šฅ์„ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ์Œ

arr ๊ฐ์ฒด์˜ ๋ชจ๋“ ์š”์†Œ๋“ค์ด callback ํ•จ์ˆ˜์— ์ˆœ์„œ๋Œ€๋กœ ํ˜ธ์ถœ๋œ๋‹ค.

for๋ฌธ์— ๋น„ํ•ด ์ข€ ๋” ๊น”๋”ํ•˜๊ณ , ์ง๊ด€์ ์ž…๋‹ˆ๋‹ค.

const arr = [0,1,2,3,4,5,6,7,8,9,10];

arr.forEach(function(element){
    console.log(element); // 0 1 2 3 4 5 6 7 8 9 10 ๊ฐ€ ์ฐจ๋ก€๋Œ€๋กœ ํ•˜๋‚˜์”ฉ ์ฐํž˜ 
});
// ํ˜น์€ arrow ํ•จ์ˆ˜ ๊ฐ€๋Šฅ
arr.forEach(element => console.log(element));

foreach() ํ•จ์ˆ˜๋Š” ๋ฆฌํ„ด๊ฐ’์ด ์—†๋‹ค.( map์ฒ˜๋Ÿผ ๊ทธ ์ž์ฒด๋กœ ์ฐ์—ˆ์„ ๋•Œ, ๊ฒฐ๊ณผ๋กœ ๋‚˜์˜ค๋Š”๊ฒŒ ์—†์Œ.)
๊ทธ๋ž˜์„œ, ๊ฒฐ๊ณผ๋กœ ๋ญ”๊ฐ€ ๋‚˜์˜ค๊ฒŒ ํ•˜๊ณ  ์‹ถ์œผ๋ฉด ๋ฐ‘์— ์ฒ˜๋Ÿผ ํ•จ์ˆ˜ ๋ฐ–์— ๋ณ€์ˆ˜๋ฅผ ์„ค์ •ํ•ด๋†”์•ผ ํ•œ๋‹ค.

forEach()๊ฐ€ ๋ฐฐ์—ด ์š”์†Œ๋งˆ๋‹ค ํ•œ ๋ฒˆ์”ฉ ์ฃผ์–ด์ง„ ํ•จ์ˆ˜(์ฝœ๋ฐฑ)๋ฅผ ์‹คํ–‰ํ•˜๋Š” ๊ฒƒ๊ณผ ๋‹ฌ๋ฆฌ,
map()์€ ๋ฐฐ์—ด ๋‚ด์˜ ๋ชจ๋“  ์š”์†Œ ๊ฐ๊ฐ์— ๋Œ€ํ•˜์—ฌ ์ฃผ์–ด์ง„ ํ•จ์ˆ˜(์ฝœ๋ฐฑ)๋ฅผ ํ˜ธ์ถœํ•œ ๊ฒฐ๊ณผ๋ฅผ ๋ชจ์•„,
์ƒˆ๋กœ์šด ๋ฐฐ์—ด์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค๋Š” ํŠน์ง•์„ ๊ฐ€์ง€๊ณ  ์žˆ๋‹ค.

const arr = [0,1,2,3,4,5,6,7,8,9,10];
const oddArray = [];

arr.forEach(function(element){
    if(element%2==1) {
        oddArray.push(element);
    }
});

console.log(oddArray); // [ 1, 3, 5, 7, 9 ]


foreach() ํ•จ์ˆ˜ ๋ฐ˜๋ณต์—์„œ ํƒˆ์ถœํ•˜๊ณ  ์‹ถ์„ ๋•Œ, return; ํ•ด์ฃผ๋ฉด ๋œ๋‹ค.

let hasC = false;
let arr = ['a', 'b', 'c', 'd'];

arr.forEach(el => {
  if (el === 'c') {
    hasC = true;
    return;
  }
});

map()ํ•จ์ˆ˜๋ž‘ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ,forEach์˜ callback ํ•จ์ˆ˜์—๋Š” ๋ฐฐ์—ด์˜ ์š”์†Œ ๋ฟ๋งŒ์•„๋‹ˆ๋ผ index, ์ „์ฒด ๋ฐฐ์—ด์„ ์ธ์ž๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

arr.forEach(function(element, index, array){
   console.log(`${array}์˜ ${index}๋ฒˆ์งธ ์š”์†Œ : ${element}`);
});
/*
0,1,2,3,4,5,6,7,8,9,10์˜ 0๋ฒˆ์งธ ์š”์†Œ : 0
0,1,2,3,4,5,6,7,8,9,10์˜ 1๋ฒˆ์งธ ์š”์†Œ : 1
0,1,2,3,4,5,6,7,8,9,10์˜ 2๋ฒˆ์งธ ์š”์†Œ : 2
0,1,2,3,4,5,6,7,8,9,10์˜ 3๋ฒˆ์งธ ์š”์†Œ : 3
0,1,2,3,4,5,6,7,8,9,10์˜ 4๋ฒˆ์งธ ์š”์†Œ : 4
0,1,2,3,4,5,6,7,8,9,10์˜ 5๋ฒˆ์งธ ์š”์†Œ : 5
0,1,2,3,4,5,6,7,8,9,10์˜ 6๋ฒˆ์งธ ์š”์†Œ : 6
0,1,2,3,4,5,6,7,8,9,10์˜ 7๋ฒˆ์งธ ์š”์†Œ : 7
0,1,2,3,4,5,6,7,8,9,10์˜ 8๋ฒˆ์งธ ์š”์†Œ : 8
0,1,2,3,4,5,6,7,8,9,10์˜ 9๋ฒˆ์งธ ์š”์†Œ : 9
0,1,2,3,4,5,6,7,8,9,10์˜ 10๋ฒˆ์งธ ์š”์†Œ : 10
*/

Array.filter()

arr.filter(callback(element[, index[, array]])[, thisArg])

filter() ๋ฉ”์†Œ๋“œ๋Š” array ๊ด€๋ จ ๋ฉ”์„œ๋“œ๋กœ ์กฐ๊ฑด์— ๋งž๋Š” ์š”์†Œ๋“ค๋งŒ ๋ชจ์•„์„œ ์ƒˆ๋กœ์šด ๋ฐฐ์—ด์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
๋งŒ์•ฝ ์กฐ๊ฑด์— ๋ถ€ํ•ฉ๋˜๋Š” ์š”์†Œ๊ฐ€ ์•„๋ฌด๊ฒƒ๋„ ์—†๋‹ค๋ฉด ๋นˆ ๋ฐฐ์—ด์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

filter() ๋ฉ”์†Œ๋“œ๋„ map() ๋ฉ”์„œ๋“œ์™€ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ํฌ๊ฒŒ filter(callbackFunction, thisAgr) 2๊ฐœ์˜ ์ธ์ž๋ฅผ ๊ฐ€์ง„๋‹ค.
๊ทธ๋ฆฌ๊ณ  callbackFunction ์•ˆ์—์„œ 3๊ฐœ์˜ ์ธ์ž (element, index, array) ๋ฅผ ๊ฐ€์ง€๋Š”๋ฐ
์ฒซ๋ฒˆ์งธ ๋ถ€๋ถ„์ธ element ์ธ์ž๋งŒ ํ•„์ˆ˜๋กœ ์ง€์ •๋˜์–ด์•ผ ํ•˜๊ณ  ๋‚˜๋จธ์ง€๋Š” ์„ ํƒ์ ์ด๋‹ค.

value > 10 ์ด๋ผ๋Š” ์กฐ๊ฑด์— ๋งž๋Š” ์š”์†Œ๋“ค๋กœ๋งŒ ์ด๋ฃจ์–ด์ง„ ์ƒˆ๋กœ์šด ๋ฐฐ์—ด์ด ์ƒ๊ฒผ๋‹ค.

let numbers = [10, 4, 32, 17, 5, 2];
โ€‹
// ์ฒซ๋ฒˆ์งธ ๋ฐฉ๋ฒ• (filter()์˜ ์ธ์ž์—์„œ ๋ฐ”๋กœ ํ•จ์ˆ˜๋ฅผ ์จ์ฃผ๋Š” ๋ฐฉ๋ฒ•) 
let result = numbers.filter(value=> value > 10);
โ€‹
console.log(result);      // [ 32, 17 ]
โ€‹
// ๋‘๋ฒˆ์งธ ๋ฐฉ๋ฒ• (๋ฐ–์—์„œ ํ•จ์ˆ˜๋ฅผ ์„ ์–ธํ•˜๊ณ  filter()์ธ์ž์—์„œ callbackํ•˜๋Š” ๋ฐฉ๋ฒ•) 
function isBiggerThanTen (value) {
   return value > 10;
}
โ€‹
let result = numbers.filter(isBiggerThanTen);
โ€‹
console.log(result);      // [ 32, 17 ]

ex)
fruits ๋ผ๋Š” ๋ฐฐ์—ด์ด ์žˆ์Šต๋‹ˆ๋‹ค.
'ap'๊ฐ€ ๋“ค์–ด๊ฐ„ ๊ณผ์ผ๋“ค๋กœ ์ด๋ฃจ์–ด์ง„ ์ƒˆ๋กœ์šด ๋ฐฐ์—ด์„ filter()๋ฅผ ์ด์šฉํ•˜์—ฌ ๋ฐ˜ํ™˜ํ•ด์ฃผ์„ธ์š”.

๊ฒฐ๊ณผ๋Š” ์•„๋ž˜์™€ ๊ฐ™์•„์•ผ ํ•ฉ๋‹ˆ๋‹ค.

[ 'apple', 'grapes' ]

ํžŒํŠธ : includes()๋ฅผ ์‚ฌ์šฉํ•ด๋ณด์„ธ์š”.


function filtered (value) {


  return value.includes('ap');
  
}
let result = fruits.filter(filtered);

console.log(result);

ex2)
courses ๋ผ๋Š” ๋ฐฐ์—ด์ด ์žˆ์Šต๋‹ˆ๋‹ค. level์ด 'hard'์ธ ๊ณผ๋ชฉ๋“ค๋กœ๋งŒ ์ด๋ฃจ์–ด์ง„ ์ƒˆ๋กœ์šด ๋ฐฐ์—ด์„ filter()๋ฅผ ์ด์šฉํ•˜์—ฌ ๋ฐ˜ํ™˜ํ•ด์ฃผ์„ธ์š”.๊ฒฐ๊ณผ๋Š” ์•„๋ž˜์™€ ๊ฐ™์•„์•ผ ํ•ฉ๋‹ˆ๋‹ค.

[
{ level: 'hard', subject: 'Mathmatic' },
{ level: 'hard', subject: 'Science' }
]

function filtered(value) {
 return value.level === 'hard';
}
let a = courses.filter(filtered);

console.log(a);

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