"JavaScript 배열 n번째 삽입"의 두 판 사이의 차이

2번째 줄: 2번째 줄:
;자바스크립트 배열 n번째 삽입
;자바스크립트 배열 n번째 삽입


==1개 삽입==
<syntaxhighlight lang='javascript'>
<syntaxhighlight lang='javascript'>
// n번째에 삽입
// n번째에 element 삽입
arr.splice(n, 0, new_obj);
arr.splice(n, 0, element);
</syntaxhighlight>
</syntaxhighlight>
<syntaxhighlight lang='javascript' run>
<syntaxhighlight lang='javascript' run>
var fruits = ["Apple", "Banana", "Orange", "Mango"];
const fruits = ["Apple", "Banana", "Orange", "Mango"];
var new_fruit = "Melon";
const element = "Melon";
fruits.splice(2, 0, new_fruit);
fruits.splice(2, 0, element);
console.log( fruits );
console.log( fruits );
</syntaxhighlight>
</syntaxhighlight>


==여러 개 삽입==
<syntaxhighlight lang='javascript'>
// n번째에 elements 삽입
arr.splice(n, 0, elements);
</syntaxhighlight>
<syntaxhighlight lang='javascript' run>
<syntaxhighlight lang='javascript' run>
var fruits = ["Apple", "Banana", "Orange", "Mango"];
const fruits = ["Apple", "Banana", "Orange", "Mango"];
var new_fruits = ["Melon", "Watermelon"];
const elements = ["Melon", "Watermelon"];
fruits.splice(2, 0, new_fruits);
fruits.splice(2, 0, elements);
console.log( fruits );
console.log( fruits );
</syntaxhighlight>
</syntaxhighlight>
<syntaxhighlight lang='javascript' run>
<syntaxhighlight lang='javascript' run>
var fruits = ["Apple", "Banana", "Orange", "Mango"];
const fruits = ["Apple", "Banana", "Orange", "Mango"];
var new_fruits = ["Melon", "Watermelon"];
const elements = ["Melon", "Watermelon"];
fruits.splice(2, 0, new_fruits[0], new_fruits[1]);
fruits.splice(2, 0, elements[0], elements[1]);
console.log( fruits );
console.log( fruits );
</syntaxhighlight>
</syntaxhighlight>

2024년 1월 16일 (화) 16:09 판

1 개요

자바스크립트 배열 n번째 삽입

2 1개 삽입

// n번째에 element 삽입
arr.splice(n, 0, element);
const fruits = ["Apple", "Banana", "Orange", "Mango"];
const element = "Melon";
fruits.splice(2, 0, element);
console.log( fruits );

3 여러 개 삽입

// n번째에 elements 삽입
arr.splice(n, 0, elements);
const fruits = ["Apple", "Banana", "Orange", "Mango"];
const elements = ["Melon", "Watermelon"];
fruits.splice(2, 0, elements);
console.log( fruits );
const fruits = ["Apple", "Banana", "Orange", "Mango"];
const elements = ["Melon", "Watermelon"];
fruits.splice(2, 0, elements[0], elements[1]);
console.log( fruits );

4 같이 보기

문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}