对象数组中的jQuery筛选方法是对象的内容
os.networkInterfaces()是节点webkit应用程序中用于获取客户端计算机的网络相关数据的函数。 对我的机器来说就像 我编写了一个代码来获取客户端计算机的IPv4地址,以便在我的nw.js应用程序中使用。逻辑是,;从内部为false且族为IPv4的对象中查找地址。这是代码对象数组中的jQuery筛选方法是对象的内容,jquery,Jquery,os.networkInterfaces()是节点webkit应用程序中用于获取客户端计算机的网络相关数据的函数。 对我的机器来说就像 我编写了一个代码来获取客户端计算机的IPv4地址,以便在我的nw.js应用程序中使用。逻辑是,;从内部为false且族为IPv4的对象中查找地址。这是代码 $.each(os.networkInterfaces(),function(key,value){ $(value).each(function(index,item){ if(i
$.each(os.networkInterfaces(),function(key,value){
$(value).each(function(index,item){
if(item.internal==false && item.family=='IPv4'){
console.log(item.address); // result is "10.0.8.42" from the above picture
}
});
});
有没有其他方法可以做到这一点。在这种情况下,我们可以在这里应用jquery筛选方法吗?不要使用jquery-只需使用常规的JS Array.reduce和Array.filter:
let interfaces = os.networkInterfaces()
let matchingObjects = Object.keys(interfaces).reduce(function(matches, key) {
return matches.concat(interfaces[key].filter(function(face) {
return face.internal === false && face.family === "IPv4"
}).map(function(face) {
return face.address; //just get the address
}));
}, []);
不要使用jQuery-只需使用常规的JS Array.reduce和Array.filter:
let interfaces = os.networkInterfaces()
let matchingObjects = Object.keys(interfaces).reduce(function(matches, key) {
return matches.concat(interfaces[key].filter(function(face) {
return face.internal === false && face.family === "IPv4"
}).map(function(face) {
return face.address; //just get the address
}));
}, []);