2017-08-10 31 views
0

os.networkInterfaces()是node-webkit應用程序中用於獲取客戶機的網絡相關數據的函數。 我的機器是喜歡 enter image description here作爲對象內容的對象數組中的jQuery過濾方法

我寫代碼來獲取客戶端機器的IPv4地址在我的nw.js應用程序使用。邏輯是;找到對象地址其中內部家庭的IPv4。這是代碼。

$.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過濾器方法嗎?

回答

3

不要使用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 
    })); 
}, []);