根据键值和更改大小从数组中获取值

时间:2016-11-24 03:14:36

标签: javascript

如何获得值为&#34的问题的答案;第2类中的第二个问题?"?

我需要使用JavaScript执行此操作。类别名称将是随机的,虽然我知道这个值以及我正在寻找答案的问题的值。

类别和问题的数量会有所不同,因此解决方案不得依赖于此阵列的固定大小。

不会有任何重复的类别或问题。

任何帮助都会很棒我一直在努力解决这个问题。给我的是,类别名称正在发生变化,我不确定如何匹配一个值。

JS

{
    "Category 1":[
        {
            "question":"Fist question in category 1?",
            "answers":[
                "first answer",
                "second answer",
                "third answer",
                "fourth answer"
            ]
        },
        {
            "question":"Second question in category 1?",
            "answers":[
                "first answer",
                "second answer",
                "third answer",
                "fourth answer"
            ]
        }
    ],
    "Category 2":[
        {
            "question":"First question in category 2?",
            "answers":[
                "first answer",
                "second answer",
                "third answer",
                "fourth answer"
            ]
        },
        {
            "question":"Second question in category 2?",
            "answers":[
                "first answer",
                "second answer",
                "third answer",
                "fourth answer"
            ]
        },
        {
            "question":"Third question in category 2?",
            "answers":[
                "first answer",
                "second answer",
                "third answer",
                "fourth answer"
            ]
        }
    ]
};

7 个答案:

答案 0 :(得分:3)

您可以尝试for...in循环。

var obj = {/* Assume that this is your object */};
for (let key in obj){ //Loops through object
    for (let i = 0; i < obj[key].length; i++){ //Loops through array
        if (obj[key][i].question == "Second question in category 2?"){
            var answers = obj[key][i].answers; //Here's your answers in array
        }
    }
}

答案 1 :(得分:1)

对类别执行for循环,然后为每个类别执行for循环。

for (var i in categories) {
    var category = categories[i];
    for (var k in category) {
        if (category[k].question == 'First question in category 2?') {
            console.log(category[k]);
        }
    }
}

答案 2 :(得分:0)

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
import java.util.ArrayList;
import java.util.List;

class Neighbors{
ArrayList<Integer> inner = new ArrayList();
Neighbors(){}
}

ArrayList<Neighbors> outer = new ArrayList<Neighbors>();

void setup() {
size(1280, 700, JAVA2D);
background(0);

Neighbors test1 = new Neighbors();
Neighbors test2 = new Neighbors();

test1.inner.add(0);
test1.inner.add(1);
test1.inner.add(2);

test2.inner.add(5);
test2.inner.add(6);
test2.inner.add(7);

println(test1.inner);
println(test2.inner);
//  outer.add((ArrayList)test.inner);
outer.add(test1.inner);
outer.add(test2.inner);
println(outer);

}

答案 3 :(得分:0)

试试这段代码:

&#13;
&#13;
const obj = {
    "Category 1": [{
        "question": "Fist question in category 1?",
        "answers": ["first answer",
            "second answer",
            "third answer",
            "fourth answer"
        ]
    }, {
        "question": "Second question in category 1?",
        "answers": ["first answer",
            "second answer",
            "third answer",
            "fourth answer"
        ]
    }],
    "Category 2": [{
        "question": "First question in category 2?",
        "answers": ["first answer",
            "second answer",
            "third answer",
            "fourth answer"
        ]
    }, {
        "question": "Second question in category 2?",
        "answers": [ "first answer",
            "second answer",
            "third answer",
            "fourth answer",
        ]
    }, {
        "question": "Third question in category 2?",
        "answers": ["first answer",
            "second answer",
            "third answer",
            "fourth answer"
        ]
    }]
};

const values = Object.values(obj);

const result = values.reduce((acc, cur) => {
  Array.prototype.push.apply(acc, cur);
  return acc;
}, []).filter(obj => obj.question === 'Third question in category 2?');

console.log(result[0]);
&#13;
&#13;
&#13;

答案 4 :(得分:0)

在每个阵列上使用 forEach 这个简单的解决方案怎么样?希望它有所帮助!

&#13;
&#13;
var obj = {
    "Category 1": [{
        "question": "Fist question in category 1?",
        "answers": ["first answer",
            "second answer",
            "third answer",
            "fourth answer"
        ]
    }, {
        "question": "Second question in category 1?",
        "answers": ["first answer",
            "second answer",
            "third answer",
            "fourth answer"
        ]
    }],

    "Category 2": [{
        "question": "First question in category 2?",
        "answers": ["first answer",
            "second answer",
            "third answer",
            "fourth answer"
        ]
    }, {
        "question": "Second question in category 2?",
        "answers": ["first answer",
            "second answer",
            "third answer",
            "fourth answer"        ]
    }, {
        "question": "Third question in category 2?",
        "answers": ["first answer",
            "second answer",
            "third answer",
            "fourth answer"
        ]
    }]
};

for(var i in obj){
    obj[i].forEach(function(x){
        if(x.question === "Second question in category 2?"){
         console.log(x.answers);
      }
    }); 
}
&#13;
&#13;
&#13;

答案 5 :(得分:0)

你可以尝试使用for循环这样的东西,

<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<div id="getAnswers"></div>
<script type="text/javascript">
var qas={
    "Category 1": [{
        "question": "Fist question in category 1?",
        "answers": ["first answer",
            "second answer",
            "third answer",
            "fourth answer"
        ]
    }, {
        "question": "Second question in category 1?",
        "answers": ["first answer",
            "second answer",
            "third answer",
            "fourth answer"
        ]
    }],
    "Category 2": [{
        "question": "First question in category 2?",
        "answers": ["first answer",
            "second answer",
            "third answer",
            "fourth answer"
        ]
    }, {
        "question": "Second question in category 2?",
        "answers": ["first answer",
            "second answer",
            "third answer",
            "fourth answer"
        ]
    }, {
        "question": "Third question in category 2?",
        "answers": ["first answer",
            "second answer",
            "third answer",
            "fourth answer"
        ]
    }]
};

for(obj in qas){
	var qasobj=qas[obj];
	for(categ in qasobj){
		console.log("getting second"+qasobj[categ].question);
		if(qasobj[categ].question=="Second question in category 2?"){
			console.log("You have got your answers "+qasobj[categ].answers);
			document.getElementById("getAnswers").innerHTML=qasobj[categ].answers;
		}
	}
}
 </script>
</body>
</html>

答案 6 :(得分:0)

您也可以使用cross-stitch it

&#13;
&#13;
var questions = {
  "Category 1": [{
    "question": "Fist question in category 1?",
    "answers": ["first answer",
      "second answer",
      "third answer",
      "fourth answer"
    ]
  }, {
    "question": "Second question in category 1?",
    "answers": ["first answer",
      "second answer",
      "third answer",
      "fourth answer"
    ]
  }],
  "Category 2": [{
    "question": "First question in category 2?",
    "answers": ["first answer",
      "second answer",
      "third answer",
      "fourth answer"
    ]
  }, {
    "question": "Second question in category 2?",
    "answers": ["first answer",
      "second answer",
      "third answer",
      "fourth answer"
    ]
  }, {
    "question": "Third question in category 2?",
    "answers": ["first answer - 1",
      "second answer",
      "third answer",
      "fourth answer"
    ]
  }]
};


function getAnswer(inputQuestion) {
  var output = {};

  Object.keys(questions).forEach((category) => {
    questions[category].forEach((val) => {
      if (val.question === inputQuestion) {
        output = val.answers;
      }
    });
  });
  return output;
}


var answer = getAnswer("Third question in category 2?");
console.log(answer);
&#13;
&#13;
&#13;