[AVASCRIPT]配列の要素-UNSHIFT()、PUSH()、SHIFT()、POP()の追加と削除
アルゴリズムの問題を解き始めるとともに、基本的なJavaScriptの方法を再整理すれば良いので、整理を始めましょう!
まず、JavaScript配列の要素を追加および削除する方法について説明します.アレイ要素のシフトなし()を追加し、push() アレイ要素shift()、pop() を削除
1.配列への要素の追加
まず、JavaScript配列の要素を追加および削除する方法について説明します.
1.配列への要素の追加
unshift()
配列の先頭に1つ以上の要素を追加し、配列の長さを返します.const users = ['Charles', 'Cecilia', 'Den'];
const totalUsers = users.unshift('Sherlock', 'John');
console.log(totalUsers); // 5 => 배열의 길이를 반환한다.
console.log(users); // ['Sherlock', 'John', 'Charles', 'Cecilia', 'Den'] => Sherlock과 'John'이 배열의 첫부분에 추가되었다.
push()
配列の最後に1つ以上の要素を追加し、配列の長さを返します.const users = ['Charles', 'Cecilia', 'Den'];
const totalUsers = users.push('Sherlock', 'John');
console.log(totalUsers); // 5 => 배열의 길이를 반환한다.
console.log(users); // ['Charles', 'Cecilia', 'Den', 'Sherlock', 'John'] => Sherlock과 'John'이 배열의 마지막에 추가되었다.
2.配列内の要素の削除
shift()
配列の最初の要素を除去し、除去した要素を返します.const users = [ 'Sherlock', 'John', 'Moriarty' ];
const newUsers = users.shift();
console.log(newUsers); // 'Sherlock' => 배열의 첫 번째 요소인 'Sherlock'가 반환된다.
console.log(users); // [ 'John', 'Moriarty' ]
pop()
配列の最後の要素を除去し、除去した要素を返します.const users = ['Sherlock', 'John', 'Moriarty'];
const newUsers = users.pop();
console.log(newUsers); // 'Moriarty' => 배열의 마지막에 있는 'Moriarty'가 반환된다.
console.log(users); // [ 'Sherlock', 'John' ]
cpop()とshift()を使用してアレイ内のすべての要素を削除
shift()、pop()に削除する要素がない場合は、配列内のすべての要素を次のように削除する未定義のプロパティを返すことができます.let names = ["Andrew", "Edward", "Paul", "Chris" ,"John"];
while( (i = names.pop()) !== undefined ) {
console.log('제거된요소',i);
console.log('변경된배열', names)
}
//console.log()의 결과 값들
'제거된요소' 'John'
'변경된배열' [ 'Andrew', 'Edward', 'Paul', 'Chris' ]
'제거된요소' 'Chris'
'변경된배열' [ 'Andrew', 'Edward', 'Paul' ]
'제거된요소' 'Paul'
'변경된배열' [ 'Andrew', 'Edward' ]
'제거된요소' 'Edward'
'변경된배열' [ 'Andrew' ]
'제거된요소' 'Andrew'
'변경된배열' []
```![](https://media.vlpt.us/images/dev_cecy/post/8bc9fab9-8e9c-4e33-ab5e-301fb208c8ad/%E1%84%89%E1%85%B3%E1%84%8F%E1%85%B3%E1%84%85%E1%85%B5%E1%86%AB%E1%84%89%E1%85%A3%E1%86%BA%202022-03-22%20%E1%84%8B%E1%85%A9%E1%84%92%E1%85%AE%201.53.02.png)
Reference
この問題について([AVASCRIPT]配列の要素-UNSHIFT()、PUSH()、SHIFT()、POP()の追加と削除), 我々は、より多くの情報をここで見つけました
https://velog.io/@dev_cecy/JAVASCRIPT-배열의-요소-추가-및-삭제-UNSHIFT-PUSH-SHIFT-POP
テキストは自由に共有またはコピーできます。ただし、このドキュメントのURLは参考URLとして残しておいてください。
Collection and Share based on the CC Protocol
const users = ['Charles', 'Cecilia', 'Den'];
const totalUsers = users.unshift('Sherlock', 'John');
console.log(totalUsers); // 5 => 배열의 길이를 반환한다.
console.log(users); // ['Sherlock', 'John', 'Charles', 'Cecilia', 'Den'] => Sherlock과 'John'이 배열의 첫부분에 추가되었다.
const users = ['Charles', 'Cecilia', 'Den'];
const totalUsers = users.push('Sherlock', 'John');
console.log(totalUsers); // 5 => 배열의 길이를 반환한다.
console.log(users); // ['Charles', 'Cecilia', 'Den', 'Sherlock', 'John'] => Sherlock과 'John'이 배열의 마지막에 추가되었다.
shift()
配列の最初の要素を除去し、除去した要素を返します.
const users = [ 'Sherlock', 'John', 'Moriarty' ];
const newUsers = users.shift();
console.log(newUsers); // 'Sherlock' => 배열의 첫 번째 요소인 'Sherlock'가 반환된다.
console.log(users); // [ 'John', 'Moriarty' ]
pop()
配列の最後の要素を除去し、除去した要素を返します.
const users = ['Sherlock', 'John', 'Moriarty'];
const newUsers = users.pop();
console.log(newUsers); // 'Moriarty' => 배열의 마지막에 있는 'Moriarty'가 반환된다.
console.log(users); // [ 'Sherlock', 'John' ]
cpop()とshift()を使用してアレイ内のすべての要素を削除
shift()、pop()に削除する要素がない場合は、配列内のすべての要素を次のように削除する未定義のプロパティを返すことができます.
let names = ["Andrew", "Edward", "Paul", "Chris" ,"John"];
while( (i = names.pop()) !== undefined ) {
console.log('제거된요소',i);
console.log('변경된배열', names)
}
//console.log()의 결과 값들
'제거된요소' 'John'
'변경된배열' [ 'Andrew', 'Edward', 'Paul', 'Chris' ]
'제거된요소' 'Chris'
'변경된배열' [ 'Andrew', 'Edward', 'Paul' ]
'제거된요소' 'Paul'
'변경된배열' [ 'Andrew', 'Edward' ]
'제거된요소' 'Edward'
'변경된배열' [ 'Andrew' ]
'제거된요소' 'Andrew'
'변경된배열' []
```![](https://media.vlpt.us/images/dev_cecy/post/8bc9fab9-8e9c-4e33-ab5e-301fb208c8ad/%E1%84%89%E1%85%B3%E1%84%8F%E1%85%B3%E1%84%85%E1%85%B5%E1%86%AB%E1%84%89%E1%85%A3%E1%86%BA%202022-03-22%20%E1%84%8B%E1%85%A9%E1%84%92%E1%85%AE%201.53.02.png)
Reference
この問題について([AVASCRIPT]配列の要素-UNSHIFT()、PUSH()、SHIFT()、POP()の追加と削除), 我々は、より多くの情報をここで見つけました https://velog.io/@dev_cecy/JAVASCRIPT-배열의-요소-추가-및-삭제-UNSHIFT-PUSH-SHIFT-POPテキストは自由に共有またはコピーできます。ただし、このドキュメントのURLは参考URLとして残しておいてください。
Collection and Share based on the CC Protocol