0

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 
5
  • what you are trying with Object.keys(data.questions[0][0])? where data.questions[0][0] is 17 so what you meant with Object.keys(17)? Commented Aug 23, 2017 at 9:48
  • @KoushikChatterjee Object.keys(...) is working for me for count total question and also total parameter in one question now I want to get the value of parameter 0 and parameter 1. Where I am stuck right now. Commented Aug 23, 2017 at 9:52
  • console.log(data.questions["0"]["1"]); this should give the results. Commented Aug 23, 2017 at 9:52
  • @Dinesh this returns 1 not the value 12. Commented Aug 23, 2017 at 9:53
  • @KirankumarDafda check my answer Commented Aug 23, 2017 at 9:54

5 Answers 5

1

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"]);

Sign up to request clarification or add additional context in comments.

3 Comments

@KirankumarDafda did you compare his answer and my answer properly? He actually modified your data and wrote answer.
Yes, she edited her answer. But both of your answer giving me same response which is [object,object] and 0 and 1
Then there might be a other problem. Please debug your code.
1

To get length of any particular question (in your data structure) use

Object.keys(data.questions["0"]) or 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..

2 Comments

Not working for me data.questions[0][0] returns 0 and data.questions[0][1] returns 0. while I need 17 and 12
It should work, try pressing Run code snippet button on Nina's answer
1

You get the result without 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; }

2 Comments

I need Object.key to get length of questions. Also data.questions[0] returns [object,object] and another two 0 and 1 value only
How to get the value of keys which is 17 and 12 for first question.
0

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

1 Comment

No, I am not able to change response of API so I need to adjust data from this detail only.
0

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

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"]);

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.