Reputation: 382
I have the file default.json
:
{
"IPs": {
"ip1": "192.168.0.1",
"ip2": "192.168.0.2",
"ip3": "192.168.0.3"
}
}
My code:
var _ = require('underscore'),
config = require('default.json')
var array = ['192.168.0.1', '192.168.0.2']
//search array in IPs
How can I search values of array
in IPs
(ip1
, ip2
, ip3
) and whenever it's true, call a function? Preferably using an underscore function.
Upvotes: 0
Views: 670
Reputation: 382
I used this solution (with underscore
):
File default.json
{
"IPs": {
"ip1": "192.168.0.1",
"ip2": "192.168.0.2",
"ip3": "192.168.0.3"
}
}
The code:
var _ = require('underscore'),
config = require('default.json')
var array = ['192.168.0.1', '192.168.0.2']
var IPs = config.IPs
_.each(IPs, function(e, k){
//if find IPs in array
if(array.indexOf(e) > -1)
//do something
else
//do something
})
Upvotes: 0
Reputation: 3781
Assuming you fix default.json to be like this:
"IPs": {
ip1: "192.168.0.1",
ip2: "192.168.0.2",
ip3: "192.168.0.3"
}
You can then search for the IPs matching
var foundIPs = Object.keys(config.IPs).filter(function(name) {
// name will be something like 'ip1', 'ip2', or 'ip3'
var currIP = config.IPs[name]; // eg. currIP = "192.168.0.1"
var inArray = array.indexOf(currIP) > -1;
return inArray;
});
var foundIP = foundIPs.length > 0;
Upvotes: 1