【问题标题】:Getting a string from JSON object with args from another object从 JSON 对象获取字符串,并从另一个对象获取 args
【发布时间】:2019-09-03 10:48:17
【问题描述】:

假设我有一个这样的 JSON 文件:

{"ABCD":{
    "1_99":{"type": 3, "serverPath": "http://some.website.com"}
  },
 "EFGH":{
    "1_00":{"type": 2, "serverPath": "http://another.meme.website/"}
    "1_01":{"type": 2, "serverPath": "http://yet.another.website.com/memes"}
  },
  etc..
}

我想引用该文件中的元素之一:

let appList = require('./config/appList.json');
...
var uri = appList.ABCD.1_99.serverPath;

其中元素名称“ABCD”和“1_99”来自另一个对象,并且并不总是相同,比如:

var uri = appList. [httpPOSTRequest.app_id] . [httpPOSTRequest.app_ver] . serverPath;

请问有什么办法可以做到的。

【问题讨论】:

  • 离你不远了。尝试像var uri = gameList[ key1 ][ key2 ].serverPath;这样的格式。
  • 你好大卫,阅读更多关于属性访问器的信息developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
  • 更好的是,在取消对下一个引用之前,通过验证检查每个引用重新编写。所以检查 (gameList[key1] != undefined) 然后 (gameList[key1][key2] != undefined) 最后完整参考。

标签: node.js json


【解决方案1】:

您可以循环访问您的 appList,也可以使用其他库,例如 (https://lodash.com/)。我用简单的循环实现了它

let appList = {
    "ABCD": {
        "1_99": {
            "type": 3,
            "serverPath": "http://some.website.com"
        }
    },
    "EFGH": {
        "1_00": {
            "type": 2,
            "serverPath": "http://another.meme.website/"
        }
    }
}


Object.keys(appList).map((d,i)=>{
    Object.keys(appList[d]).map((data, index)=>{
        console.log(`server path ${i} : ${appList[d][data].serverPath}`);
    })
})

【讨论】:

    【解决方案2】:

    您可以将您的搜索属性封装在一个搜索对象中,然后通过 JavaScript bracket notation 使用它来访问您的 appList 对象。

    我已将其封装在一个 getServerPath 函数中,该函数也传达了练习的意图。

    如果找不到相关路径,我们将返回 null

    另外,请记住,lodash_.get 中有一个非常有用的函数,它可以为您做很多这样的事情,我在下面提供了一个示例。

    希望对你有帮助!

    // Get the required server path given the searchInput object
    function getServerPath(appList, searchInput) {
        if (!searchInput) return null;
        if (!appList[searchInput.property1]) return null;
        if (!appList[searchInput.property1][searchInput.property2]) return null;
        return appList[searchInput.property1][searchInput.property2].serverPath;
    }
    
    // In Node.js we would use require('file.json') to define this.
    let appList = {
        "ABCD": {
            "1_99": {
                "type": 3,
                "serverPath": "http://some.website.com"
            }
        },
        "EFGH": {
            "1_00": {
                "type": 2,
                "serverPath": "http://another.meme.website/"
            }
        }
    }
    
    // Search object input
    let searchInput1 = { property1: "ABCD", property2: "1_99" };
    let searchInput2 = { property1: "EFGH", property2: "1_00" };
    let searchInput3 = { property1: "DOESNTEXIST", property2: "1_00" };
    
    console.log("Server path 1:", getServerPath(appList, searchInput1));
    console.log("Server path 2:", getServerPath(appList, searchInput2));
    console.log("Server path 3:", getServerPath(appList, searchInput3));
    
    // Using the very useful lodash _.get
    console.log("Server path (using lodash _.get):", _.get(appList, "ABCD.1_99.serverPath"));
    <script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.15/lodash.js"></script>

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-08
      相关资源
      最近更新 更多