如何从与 JavaScript (JSON) 在同一行的另一个值中找到值?
How can I find the value from another value in the same line with JavaScript (JSON)?
我正在尝试从数字中获取“correctAnswers”。
这里是 JSON:
的样本
{
"questions": [
{
"number": 3,
"question": "☀️ ➕ = ?",
"answers": [
"sunflower",
"BIRTHDAY",
"iPhone",
"MOON CAKE"
],
"correctAnswers": [
"sunflower"
],
"random": true,
"timeLimit": "24"
},
{
"number": 1,
"question": "⭐️ ➕ =❓ ",
"answers": [
"STAR FISH",
"iPhone",
"MOON CAKE",
"HOT DOG"
],
"correctAnswers": [
"STAR FISH"
],
"random": true,
"timeLimit": "20"
},
{
"number": 7,
"question": " ➕ ☎ = ",
"answers": [
"SUNGLASSES",
"SUNFLOWER",
"HOUSEPAINT",
"IPHONE"
],
"correctAnswers": [
"IPHONE"
],
"random": true,
"timeLimit": 15
},
]
}
我试图在“问题”中找到“数字”:1 或随机数,然后从该数字中找到“正确答案”。
const array = {
"questions": [
{
"number": 3,
"question": "☀️ ➕ = ?",
"answers": [
"sunflower",
"BIRTHDAY",
"iPhone",
"MOON CAKE"
],
"correctAnswers": [
"sunflower"
],
"random": true,
"timeLimit": "24"
},
{
"number": 1,
"question": "⭐️ ➕ =❓ ",
"answers": [
"STAR FISH",
"iPhone",
"MOON CAKE",
"HOT DOG"
],
"correctAnswers": [
"STAR FISH"
],
"random": true,
"timeLimit": "20"
},
{
"number": 7,
"question": " ➕ ☎ = ",
"answers": [
"SUNGLASSES",
"SUNFLOWER",
"HOUSEPAINT",
"IPHONE"
],
"correctAnswers": [
"IPHONE"
],
"random": true,
"timeLimit": 15
},
]
};
const { questions } = array;
// Find by number
const correctAnswersByNumber = qNumber => {
const q = questions.find(({ number }) => number === qNumber);
if (!q) {
return false;
}
return q.correctAnswers;
};
console.log(correctAnswersByNumber(3)); // Question by number === 3
// Random question
const randomCorrectAnswers = questions[Math.floor(Math.random() * questions.length)].correctAnswers;
console.log(randomCorrectAnswers);
使用随机选择一个随机问题。然后显示正确的属性。
var data = {
"questions": [
{
"number": 3,
"question": "☀️ ➕ = ?",
"answers": [
"sunflower",
"BIRTHDAY",
"iPhone",
"MOON CAKE"
],
"correctAnswers": [
"sunflower"
],
"random": true,
"timeLimit": "24"
},
{
"number": 1,
"question": "⭐️ ➕ =❓ ",
"answers": [
"STAR FISH",
"iPhone",
"MOON CAKE",
"HOT DOG"
],
"correctAnswers": [
"STAR FISH"
],
"random": true,
"timeLimit": "20"
},
{
"number": 7,
"question": " ➕ ☎ = ",
"answers": [
"SUNGLASSES",
"SUNFLOWER",
"HOUSEPAINT",
"IPHONE"
],
"correctAnswers": [
"IPHONE"
],
"random": true,
"timeLimit": 15
},
]
};
// Let's choose a random question
let question = data['questions'][Math.floor(Math.random() * data['questions'].length)];
console.log(question.question, question.correctAnswers)
我正在尝试从数字中获取“correctAnswers”。
这里是 JSON:
的样本{
"questions": [
{
"number": 3,
"question": "☀️ ➕ = ?",
"answers": [
"sunflower",
"BIRTHDAY",
"iPhone",
"MOON CAKE"
],
"correctAnswers": [
"sunflower"
],
"random": true,
"timeLimit": "24"
},
{
"number": 1,
"question": "⭐️ ➕ =❓ ",
"answers": [
"STAR FISH",
"iPhone",
"MOON CAKE",
"HOT DOG"
],
"correctAnswers": [
"STAR FISH"
],
"random": true,
"timeLimit": "20"
},
{
"number": 7,
"question": " ➕ ☎ = ",
"answers": [
"SUNGLASSES",
"SUNFLOWER",
"HOUSEPAINT",
"IPHONE"
],
"correctAnswers": [
"IPHONE"
],
"random": true,
"timeLimit": 15
},
]
}
我试图在“问题”中找到“数字”:1 或随机数,然后从该数字中找到“正确答案”。
const array = {
"questions": [
{
"number": 3,
"question": "☀️ ➕ = ?",
"answers": [
"sunflower",
"BIRTHDAY",
"iPhone",
"MOON CAKE"
],
"correctAnswers": [
"sunflower"
],
"random": true,
"timeLimit": "24"
},
{
"number": 1,
"question": "⭐️ ➕ =❓ ",
"answers": [
"STAR FISH",
"iPhone",
"MOON CAKE",
"HOT DOG"
],
"correctAnswers": [
"STAR FISH"
],
"random": true,
"timeLimit": "20"
},
{
"number": 7,
"question": " ➕ ☎ = ",
"answers": [
"SUNGLASSES",
"SUNFLOWER",
"HOUSEPAINT",
"IPHONE"
],
"correctAnswers": [
"IPHONE"
],
"random": true,
"timeLimit": 15
},
]
};
const { questions } = array;
// Find by number
const correctAnswersByNumber = qNumber => {
const q = questions.find(({ number }) => number === qNumber);
if (!q) {
return false;
}
return q.correctAnswers;
};
console.log(correctAnswersByNumber(3)); // Question by number === 3
// Random question
const randomCorrectAnswers = questions[Math.floor(Math.random() * questions.length)].correctAnswers;
console.log(randomCorrectAnswers);
使用随机选择一个随机问题。然后显示正确的属性。
var data = {
"questions": [
{
"number": 3,
"question": "☀️ ➕ = ?",
"answers": [
"sunflower",
"BIRTHDAY",
"iPhone",
"MOON CAKE"
],
"correctAnswers": [
"sunflower"
],
"random": true,
"timeLimit": "24"
},
{
"number": 1,
"question": "⭐️ ➕ =❓ ",
"answers": [
"STAR FISH",
"iPhone",
"MOON CAKE",
"HOT DOG"
],
"correctAnswers": [
"STAR FISH"
],
"random": true,
"timeLimit": "20"
},
{
"number": 7,
"question": " ➕ ☎ = ",
"answers": [
"SUNGLASSES",
"SUNFLOWER",
"HOUSEPAINT",
"IPHONE"
],
"correctAnswers": [
"IPHONE"
],
"random": true,
"timeLimit": 15
},
]
};
// Let's choose a random question
let question = data['questions'][Math.floor(Math.random() * data['questions'].length)];
console.log(question.question, question.correctAnswers)