我收到兩個IP地址陣列,這些陣列的格式不同。應該從地址數組中刪除IP數組中的任何值 - 但只有在IP匹配完全相同的情況下。我寫了下面,但問題是,例如,192.168.0.1將匹配192.168.0.11,然後從地址數組中刪除192.168.0.11,這是不是一個有效的結果。地址數組需要以與接收到的格式相同的格式返回。請幫忙嗎? :)嘗試匹配兩個數組中的值,並且只在部分值存在完全匹配時才刪除
var addresses = [{
Value : '192.168.0.11'
}, {
Value : '52.210.29.181'
}, {
Value : '52.210.128.97'
}
];
var IPs = ['192.168.0.1', '52.210.128.97'];
console.log('Before:', addresses);
for (var x = 0; x < IPs.length; x++) {
for (var key in addresses) {
var address = JSON.stringify(addresses[key]);
if (address.indexOf(IPs[x]) > -1){ //if the IP is a substr of address
console.log('matched, so delete', addresses[key]);
var index = addresses.indexOf(addresses[key]); //find the index of IP to be deleted then delete it
addresses.splice(index, 1);
}
}
}
console.log('After', addresses);
地址是你似乎是治療對象的字符串數組的字符串。嘗試地址==(或可能是===)IP [x]是否合乎邏輯? –