λ°°μ΄ μμ κ°μ²΄μ΄κΈ°μ delete
λ‘ κ°μ μ§μΈ μλ μμ§λ§ μ§μ΄ μμμλ¦¬κ° λΉ μλ¦¬κ° λλ€.
let arr = ['1', '2', '3', '4', '5',];
delete arr[1];
console.log(arr) // ['1', empty, '3', '4', '5']
μ΄μ λΉν΄ splice
λ μμ μΆκ°, μ κ±°, κ΅μ²΄κΉμ§ κ°λ₯νλ©°,
λ¬Έλ²μ arr.splice(index, deleteCount, 'ele1', 'ele2')
μ΄λ€.
index
μ μμΉλΆν° deleteCount
μ μ λ§νΌμ μμλ₯Ό μμ νκ³ 'ele1'
κ³Ό 'ele2'
λ₯Ό index
μ μμΉμ μΆκ°νλ€λ λ»μ΄λ€.
μΈμλ‘ index
μ deleteCount
λ§ λ°λ‘ μ¬μ©ν μ μκ³ , deleteCount
λ₯Ό 0μΌλ‘ νλ©΄ μμμΆκ°λ§ νλ€.
κ·Έλ¦¬κ³ splice λ©μλλ₯Ό μ¬μ©ν λ μμλ₯Ό μμ νλ©΄ μμ ν μμλ₯Ό λ°ννκ³ μμ μΆκ°λ§ νλ€λ©΄ λΉ λ°°μ΄
μ λ°ννλ€.
μΈμλ‘ μμλ₯Ό λ°μ μ μμΌλ©° μμλ₯Ό μ¬μ©ν κ²½μ° λ°°μ΄μ λ€μμλΆν° μΌλ€.
let arr = ['1', '2', '3', '4', '5',];
arr.splice(1,2) // ['2', '3']; μμ μΆκ°νμ§ μκ³ μ κ±°λ§ νλ κ²½μ°
console.log(arr) // ['1', '4', '5']
arr.splice(1, 0, '2', '3') // []; deleteCountλ₯Ό 0μΌλ‘ μ€μ μμ μΆκ°λ§ νλ κ²½μ°
console.log(arr) // ['1', '2', '3', '4', '5']
arr.splice(-3, 2) // ['3', '4']; indexλ₯Ό μμλ‘ μ£Όλ κ²½μ°
console.log(arr) // ['1', '2', '5']
slice
λ©μλλ splice
λ©μλλ³΄λ€ κ°λ¨νλ°, startIndex
λΆν° endIndex
κΉμ§μ λ°°μ΄μ 볡μ¬ν΄ λ°ννλ€.
splice
λ©μλλ μ€μ§μ μΌλ‘ λ°°μ΄μ μμμ μν₯μ μ£Όλ λ°λ©΄ slice
λ©μλλ 볡μ¬ν΄ λ°ννκΈ°μ μ λ°°μ΄μ μμμ μν₯μ μ£Όμ§ μλλ€.
let arr = ['1', '2', '3', '4', '5',];
arr.slice(0,3) // ['1', '2', '3']; startIndexλΆν° endIndexκΉμ§μ΄μ§λ§ endIndexλ μ μΈνκ³ λ°ννλ€.
console.log(arr) // ['1', '2', '3', '4', '5',]; slice λ©μλλ₯Ό μ¬μ©ν΄λ λ°°μ΄μ μμμ μν₯μ΄ μλ€.
arr.slice(2) // ['3', '4', '5']; μΈμλ‘ μΈλ±μ€νλλ§ λ£μΌλ©΄ κ·Έ μΈλ±μ€λΆν° λ°°μ΄ λκΉμ§ λ°ννλ€.
arr.slice() // ['1', '2', '3', '4', '5']; μΈμλ₯Ό λ£μ§ μμΌλ©΄ μ λ°°μ΄μ κ·Έλλ‘ λ³΅μ¬νλ€.
arr.slice(-2) // ['4', '5'] // μμ μΈλ±μ€λ₯Ό μ¬μ©ν μ μλ€.
arr.slice(3, 1) // []; startIndexλ³΄λ€ endIndexκ° μμ μμΌλ©΄ λΉ λ°°μ΄μ λ°ννλ€.
arr.slice(3, 10)// ['4', '5']; arr.lengthλ³΄λ€ ν° κ°μ΄ endIndex μ리μ μλ λ°°μ΄ λκΉμ§λ§ 볡μ¬νλ€.
concat
λ©μλλ κΈ°μ‘΄ λ°°μ΄μ μ΄μ©ν΄ μ λ°°μ΄μ λ§λ€κ±°λ κΈ°μ‘΄ λ°°μ΄λ€μ΄ ν©μ³μ§ λ°°μ΄μ λ§λ€λ μ¬μ©νλ€.
μΈμλ‘ λ°°μ΄μ΄λ μ¬λ¬ νμ
μ κ°μ΄ μ€λλ° μΈμμ κ°―μμ μ νμ μλ€.
concat
λ©μλλ₯Ό μ¬μ©νλ€κ³ ν΄μ μ¬μ©ν λ°°μ΄μ μμλ λ³νμ§ μμΌλ©° λ©μλλ₯Ό μ¬μ©ν κ²½μ° λ°°μ΄κ³Ό μΈμκ° ν©μ³μ§ λ°°μ΄μ λ°ννλ€.
let arr = [1, 2, 3, 4, 5,];
arr.concat([6, 7]) // [1, 2, 3, 4, 5, 6, 7]
console.log(arr) // [1, 2, 3, 4, 5]; arrμ λ³νμ§ μλλ€.
arr.concat('string', false) // [1, 2, 3, 4, 5, 'string', false] μ¬λ¬ νμ
μ μΈμλ₯Ό ν©μΉ μ μλ€.
λ°°μ΄κ³Ό κ°μ²΄λ₯Ό ν©μΉ κ²½μ° κ°μ²΄λ ν μμλ‘μ ν©μ³μ§λ€.
νμ§λ§ νΉμ νλ‘νΌν°μΈ [Symbol.isConcatSpreadable]
κ°μ΄ μ°ΈμΌλ‘ μκ³ length
λ₯Ό μ ν΄μ£Όλ©΄ κ°μ²΄ μμ²΄κ° μμκ° λλ κ²μ΄ μλ νλ‘νΌν° κ°μ΄ μμλ‘ ν©μ³μ§λ€.
let arr = [1, 2, 3,];
let object ={
1: 'value1',
2: 'value2',
3: 'value3',
}
arr.concat(object) // [1, 2, 3, {1: value1, 2: value2, 3: value3}]
let object ={
1: 'value1',
2: 'value2',
3: 'value3',
[Symbol.isConcatSpreadable]: true,
length: 2,
}
arr.concat(object) // [1, 2, 3, empty, value1]
// Symbol.isConcatSpreadable μ΄ μ°Έμ΄κ³ lengthκ° 2λΌλ©΄ objectλ μΈλ±μ€ 0μ λΉμ΄μκ³ (μ ν΄μ§μ§ μμκ³ )
// μΈλ±μ€ 1μ value1μΈ λ°°μ΄λ‘ μ·¨κΈνκ² λλ€.
// λ§μ½ κ°μ²΄μ νλ‘νΌν° ν€κ° μ«μκ° μλλΌλ©΄ μμλ‘ ν λΉλμ§ μμΌλ©° undefinedλ§ λ°νλλ€.
컨λμ μ΄ μμ’μλ μ‘°κΈμ΄λΌλ 곡λΆνλ€.
컨λμ κ΄λ¦¬λ₯Ό λͺ»ν΄ 곡λΆλ₯Ό λ무 λͺ»νλ€.
ν μμΌμ λ무 λ¬λ €μ μΌμμΌ λ΄λ΄ 컨λμ
μ΄ λ무 μμ’μλ€.
μ무리 μμ’μλ μ‘°κΈμ΄λΌλ 곡λΆνλ €κ³ νκΈ΄ νλλ° μ»¨λμ
λμ‘°μλ μ‘°κΈμ΄λΌλ 곡λΆλ₯Ό νλ€λ κ²κ³Ό 컨λμ
κ΄λ¦¬λ₯Ό λͺ»νλ€λ κ²μ΄ κ²Ήμ³μ κΈ°λΆμ΄ λ¬νλ€.
μλ€μ΄κ°λλ° κ°μ λ‘ λ³΄κ³ μλκ±°λ³΄λ¨ λΉ¨λ¦¬μκ³ μΌμ΄λμ μμΉ¨μ ν΄μΌκ² λ€.
λ΄κ° μ½μ€ μ€μ μ νλͺ¨κΈμ΄λΌλ λ§μλ©° κ°λ€. κ·Έλ¦¬κ³ κ°κ° λμλ€.
λͺ¨λ μλ°μ€ν¬λ¦½νΈ - https://ko.javascript.info/