IT story

push ()의 반대;

hot-time 2020. 6. 11. 08:23
반응형

push ()의 반대; [복제]


이 질문에는 이미 답변이 있습니다.

이 문제에 대한 도움이 필요합니다- 'JavaScript push();방법 의 반대는 무엇입니까 ?'

내가 배열을 가지고 있다고 말한 것처럼-

var exampleArray = ['remove'];

나는 push();단어를 원합니다 'keep'-

exampleArray.push('keep');

'remove'배열 에서 문자열 어떻게 삭제 합니까?


글쎄, 당신은 두 가지 질문을했습니다. push()(질문 제목) 의 반대 pop()입니다.

var exampleArray = ['myName'];
exampleArray.push('hi');
console.log(exampleArray);

exampleArray.pop();
console.log(exampleArray);

pop()마지막 요소를 제거하고 exampleArray해당 요소 ( "hi")를 반환하지만 "myName"이 마지막 요소가 아니기 때문에 배열에서 "myName"문자열을 삭제하지 않습니다.

필요한 것은 shift()또는 splice():

var exampleArray = ['myName'];
exampleArray.push('hi');
console.log(exampleArray);

exampleArray.shift();
console.log(exampleArray);

var exampleArray = ['myName'];
exampleArray.push('hi');
console.log(exampleArray);

exampleArray.splice(0, 1);
console.log(exampleArray);

더 많은 배열 방법은 https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array#Mutator_methods를 참조하십시오.


push()끝에 추가; pop()끝에서 삭제합니다.

unshift()앞에 추가; shift()앞에서 삭제합니다.

splice() 원하는 곳 어디든 원하는대로 할 수 있습니다.

참고 URL : https://stackoverflow.com/questions/25517633/opposite-of-push

반응형