본문 바로가기

Python_WEB/JavaScript

[freecodecamp]JavaScript pop()으로 배열 조작

반응형

배열의 데이터를 변경하는 또 다른 방법은 .pop () 함수를 사용하는 것입니다.

.pop ()은 배열 끝에서 값을 "팝"하는 데 사용됩니다.

이 "팝 오프"값을 변수에 할당하여 저장할 수 있습니다.

즉, .pop ()은 배열에서 마지막 요소를 제거하고 해당 요소를 반환합니다.

숫자, 문자열, 심지어 중첩 된 배열까지 모든 유형의 항목을 배열에서 "팝업"할 수 있습니다.

 

예>

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

 

Q>

1. .pop () 함수를 사용하여 myArray에서 마지막 항목을 제거하고 "popped off"값을 removedFromMyArray에 할당합니다.

2. myArray는 [[ "John", 23]] 만 포함해야합니다.

3. myArray에서 pop ()을 사용해야합니다.

4. removedFromMyArray는 [ "cat", 2] 만 포함해야합니다.

 

A>

// Setup
var myArray = [["John", 23], ["cat", 2]];

// Only change code below this line
var removedFromMyArray = myArray.pop();

 

https://www.freecodecamp.org/

 

freeCodeCamp.org

Learn to code. Build projects. Earn certifications.Since 2015, 40,000 graduates have gotten jobs at tech companies including Google, Apple, Amazon, and Microsoft.

www.freecodecamp.org

 

반응형