0

os.networkInterfaces() is a function used in node-webkit applications to get network related data of client machine. For my machine it is like enter image description here

I write a code to get IPv4 address of the client machine to use in my nw.js app. The logic is; find address from the object where internal is false and family is IPv4. this is the code.

$.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
        }
    });
});

Is there any other way to achieve this. Can we apply jquery filter methods here in this case?

1 Answer 1

3

Dont use jQuery - just use regular vanilla JS Array.reduce and 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
    }));
}, []);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.