2 배열이 있고 첫 번째 배열의 위치 번호에 따라 두 번째 배열의 요소를 제거하고 싶습니다.
var notexists = []; //value is "1","5","8" on 0,1,2nd position.
var copyrequiredfields = [];//Value is "a","b","c","d","e","f",...
for (var i = 0; i < notexists.length; i++) {
copyrequiredfields.splice(parseInt(notexists[i]), 1);
}
예를 들어 copyrequiredfields에서 1st 5th 및 8th 요소를 제거하고 싶습니다. 몇 가지 대답을 제안하십시오.
새 배열을 만들고 copyrequiredfields를 반복합니다. 인덱스가 존재하지 않으면 무시합니다.
예:
var notexist = [1,2,5];
var copyrequiredfields = ['a','b','c','d','e','f','g'];
//create a index map
var index_map = {};
for(var i = 0; i < notexist.length; i++){
index_map[notexist[i]] = true;
}
//remove the elements
var newarray = [];
for(var i = 0; i < copyrequiredfields.length; i++){
if(!index_map[i]){
newarray.push(copyrequiredfields[i]);
}
}
copyrequiredfields = newarray;
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다