gpt4 book ai didi

javascript - 使用下划线搜索数组中的 JSON 值

转载 作者:行者123 更新时间:2023-12-03 07:15:51 24 4
gpt4 key购买 nike

我有文件default.json:

{
"IPs": {
"ip1": "192.168.0.1",
"ip2": "192.168.0.2",
"ip3": "192.168.0.3"
}
}

我的代码:

var _      = require('underscore'),
config = require('default.json')

var array = ['192.168.0.1', '192.168.0.2']

//search array in IPs

如何在 IP 中搜索 array 的值(ip1ip2ip3) 每当它为 true 时,调用一个函数?最好使用下划线函数。

最佳答案

假设您将 default.json 修复为如下所示:

"IPs": {
ip1: "192.168.0.1",
ip2: "192.168.0.2",
ip3: "192.168.0.3"
}

然后您可以搜索匹配的 IP

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;

关于javascript - 使用下划线搜索数组中的 JSON 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36431986/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com