[英]Filter JSON using IP Address Range (JavaScript)
I have a JSON file where every JSON object has a unique identifier ( IP Address )我有一个 JSON 文件,其中每个 JSON 对象都有一个唯一标识符( IP 地址)
When given a range of IP Addresses, I want to filter the Original JSON based on the given range and store it in a new file.当给定一个 IP 地址范围时,我想根据给定的范围过滤原始 JSON 并将其存储在一个新文件中。 (ie All the JSON objects having an IP Address that falls in the given Range)
(即所有 JSON 对象的 IP 地址都在给定范围内)
JSON File: JSON 文件:
[{
"Name": "SERVER1",
"ipv4Address": "192.168.0.50",
"OperatingSystem": [],
"OperatingSystemServicePack": null,
"OperatingSystemVersion": "6.3 (9600)"
},
{
"Name": "SERVER2",
"ipv4Address": "192.168.0.51",
"OperatingSystem": [],
"OperatingSystemServicePack": null,
"OperatingSystemVersion": "6.3 (9600)"
},
{
"Name": "SERVER3",
"ipv4Address": "192.168.0.52",
"OperatingSystem": [],
"OperatingSystemServicePack": null,
"OperatingSystemVersion": "6.3 (9600)"
},
{
"Name": "SERVER4",
"ipv4Address": "192.168.0.53",
"OperatingSystem": [],
"OperatingSystemServicePack": null,
"OperatingSystemVersion": "6.3 (9600)"
},
{
"Name": "SERVER5",
"ipv4Address": "192.168.0.54",
"OperatingSystem": [],
"OperatingSystemServicePack": null,
"OperatingSystemVersion": "6.3 (9600)"
}]
If the given range were 192.168.0.52 - 192.168.0.54, the output should be:如果给定范围是 192.168.0.52 - 192.168.0.54,则输出应为:
[{
"Name": "SERVER3",
"ipv4Address": "192.168.0.52",
"OperatingSystem": [],
"OperatingSystemServicePack": null,
"OperatingSystemVersion": "6.3 (9600)"
},
{
"Name": "SERVER4",
"ipv4Address": "192.168.0.53",
"OperatingSystem": [],
"OperatingSystemServicePack": null,
"OperatingSystemVersion": "6.3 (9600)"
},
{
"Name": "SERVER5",
"ipv4Address": "192.168.0.54",
"OperatingSystem": [],
"OperatingSystemServicePack": null,
"OperatingSystemVersion": "6.3 (9600)"
}]
use below code snippet.使用下面的代码片段。
function isWithinRange(ip, lowerBound, upperBound) { // Put all IPs into one array for iterating and split all into their own // array of segments var ips = [ip.split('.'), lowerBound.split('.'), upperBound.split('.')]; // Convert all IPs to ints for(var i = 0; i < ips.length; i++) { // Typecast all segments of all ips to ints for(var j = 0; j < ips[i].length; j++) { ips[i][j] = parseInt(ips[i][j]); } // Bit shift each segment to make it easier to compare ips[i] = (ips[i][0] << 24) + (ips[i][1] << 16) + (ips[i][2] << 8) + (ips[i][3]); } // Do comparisons if(ips[0] >= ips[1] && ips[0] <= ips[2]) return true; return false; } var json = [{ "Name": "SERVER1", "ipv4Address": "192.168.0.50", "OperatingSystem": [], "OperatingSystemServicePack": null, "OperatingSystemVersion": "6.3 (9600)" }, { "Name": "SERVER2", "ipv4Address": "192.168.0.51", "OperatingSystem": [], "OperatingSystemServicePack": null, "OperatingSystemVersion": "6.3 (9600)" }, { "Name": "SERVER3", "ipv4Address": "192.168.0.52", "OperatingSystem": [], "OperatingSystemServicePack": null, "OperatingSystemVersion": "6.3 (9600)" }, { "Name": "SERVER4", "ipv4Address": "192.168.0.53", "OperatingSystem": [], "OperatingSystemServicePack": null, "OperatingSystemVersion": "6.3 (9600)" }, { "Name": "SERVER5", "ipv4Address": "192.168.0.54", "OperatingSystem": [], "OperatingSystemServicePack": null, "OperatingSystemVersion": "6.3 (9600)" }]; var newArr = []; var lowerBound = '192.168.0.52'; var upperBound = '192.168.0.54'; json.forEach(function(item){ var isInRange = isWithinRange(item.ipv4Address, lowerBound, upperBound); if(isInRange){ newArr.push(item); } }); console.log("new Array : ", newArr);
im using solution provided by @SaltedBlowfish using his Bit Shifting
formula我使用@SaltedBlowfish 提供的解决方案,使用他的
Bit Shifting
公式
ref: https://stackoverflow.com/a/29173281/4882013参考: https : //stackoverflow.com/a/29173281/4882013
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.