简体   繁体   English

从javascript JSON对象中查找键的值

[英]Find value of key from javascript JSON Object

I have this object value 我有这个对象的价值

var data = {
    "questions": {
        "0": {
            "0": "17",
            "1": "12"
        },
        "1": {
            "0": "22",
            "1": "34"
        },
        "2": {
            "0": "52",
            "1": "61"
        }
    }
}

I am trying to get value from these objects as I have tried below things which return me other than what I actually want. 我试图从这些对象中获取价值,因为我尝试了以下使我返回的东西,而不是我真正想要的东西。

alert(Object.keys(data.questions[0])); // Output : 0,1
alert(Object.keys(data.questions[0][0])); // Output : 0
alert(Object.keys(data.questions[0][1])); // Output : 0

Anyone can help me find the value of above keys like: 任何人都可以帮助我找到上述键的值,例如:

questions[0][0] = 17 
questions[0][1] = 12 

You get the result without Object.keys . 没有Object.keys即可得到结果。

 var data = { questions: { 0: { 0: "17", 1: "12" }, 1: { 0: "22", 1: "34" }, 2: { 0: "52", 1: "61" } } }; console.log(data.questions[0]); // { 0: "17", 1: "12" } console.log(data.questions[0][0]); // 17 console.log(data.questions[0][1]); // 12 

For searching a value's path of keys, you could use an iterative and recursive approach by checking all keys and objects. 为了搜索键的值的路径,可以通过检查所有键和对象来使用迭代和递归的方法。

 function findValue(object, value) { var p; Object.keys(object).some(function (k) { var t; if (object[k] === value) { p = [k]; return true; } if (object[k] && typeof object[k] === 'object' && (t = findValue(object[k], value))) { p = [k].concat(t); return true; } }); return p; } var data = { questions: { 0: { 0: "17", 1: "12" }, 1: { 0: "22", 1: "34" }, 2: { 0: "52", 1: "61" } } }; console.log(findValue(data, '17')); console.log(findValue(data, '34')); 
 .as-console-wrapper { max-height: 100% !important; top: 0; } 

To get length of any particular question (in your data structure) use 要获取任何特定问题的长度(在您的数据结构中),请使用

Object.keys(data.questions["0"]) or Object.keys(data.questions["1"]) Object.keys(data.questions["0"])Object.keys(data.questions["1"])

to get value of any questions use 获得使用任何问题的价值

data.questions["0"]["0"] or data.questions["0"]["1"] or data.questions["1"]["0"] and so on.. data.questions["0"]["0"]data.questions["0"]["1"]data.questions["1"]["0"]等。

Try like this. 尝试这样。

 var data = { "questions": { "0": { "0": "17", "1": "12" }, "1": { "0": "22", "1": "34" }, "2": { "0": "52", "1": "61" } } } console.log(data.questions["0"]); console.log(data.questions["0"]["0"]); console.log(data.questions["0"]["1"]); 

Can you change the data structure? 您可以更改数据结构吗? If I were you I would change it to the following: 如果我是您,请将其更改为以下内容:

 var data = { "questions": [ [17, 22], [22, 34], [52, 61] ] }; console.log(data.questions[0]); // Output : 17,22 console.log(data.questions[0][0]); // Output : 17 console.log(data.questions[1][1]); // Output : 34 

To access the first object ["0"] To access the first two object : ["0"]["0"] and so on based on the above expression we can access the objects like this 访问第一个对象["0"]要访问前两个对象: ["0"]["0"]等等,根据上面的表达式,我们可以像这样访问对象

 var data = { "questions": { "0": { "0": "17", "1": "12" }, "1": { "0": "22", "1": "34" }, "2": { "0": "52", "1": "61" } } } console.log(data.questions["0"]); console.log(data.questions["0"]["0"]); console.log(data.questions["0"]["1"]); console.log(data.questions["1"]["1"]); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM