[2016,2017,2018,2019] 배열이 있습니다.
Value 2017에서 Value 2017까지 범위가 있습니다.
따라서 fromValue와 toValue 사이의 범위를 벗어난 것을 제거해야하므로 결과 배열은 [2017]이어야합니다.
아래에 2016과 2018 만 제거하고 2019는 제거하지 않는 코드를 작성했습니다.
내가 뭘 잘못하고 있는지, 이것을하는 더 좋은 방법이 있습니까?
gNodeIDs.forEach(function (item) {
alert("Before if" + item);
if (item >= fromValue) {
if (item <= toValue) {
}
else
{
alert("removing" + item);
var index = test.indexOf(item);
if (index >= 0) {
test.splice(index, 1);
}
}
}
else {
alert("removing" + item);
var index = test.indexOf(item);
if (index >= 0) {
test.splice(index, 1);
}
}
});
이를 위해 Array.prototype.filter 를 사용하십시오 .
var result = gNodeIDs.filter(function(item) { return item >= fromValue && item <= toValue });
result
모든 일치 항목을 포함하는 배열
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다