如何在javascript中仅从join()值获取键值?
这是我的JSON。下面是我的Html5代码如何在javascript中仅从join()值获取键值?,javascript,json,node.js,html,Javascript,Json,Node.js,Html,这是我的JSON。下面是我的Html5代码 const myQuestions = [ { level:"Level 1", questionNo: 1, sentence: "Her uncles are army officers.", question: "Q . Which words are about people? ",
const myQuestions = [
{
level:"Level 1",
questionNo: 1,
sentence: "Her uncles are army officers.",
question: "Q . Which words are about people? ",
answers: {
"1": "a. uncles / officers",
"2": "b. her/are",
"3": "c. in/the"
},
correctAnswer: "1",
topic: "Noun",
description: "plural nouns"
},
{
level:"Level 1",
questionNo: 2,
sentence: "He dropped the glass and it broke into many pieces.",
question: "Q . Which word stands for 'the glass'?",
answers: {
"1": "a. he",
"2": "b. it",
"3": "c. into"
},
correctAnswer: "2",
topic: "Pronoun",
description: "pronoun 'it' and what it has already referred to"
},
....
]
${answers.join(“”}
现在我们得到的键和值也是这样
1:a。叔叔/官员
但我们只想要价值观。任何人都可以解决此错误?您可以使用
Object.values()
来解决此问题:
<div class="answers"> ${answers.join("")} </div>
const myQuestions=[{
级别:“级别1”,
问题1:,
判决:“她的叔叔是军官。”,
问题:“问:哪些词是关于人的?”,
答复:{
“1”:“a.叔叔/官员”,
“2”:“b.她/你”,
“3”:“c.in/the”
},
正确答案:“1”,
主题:“名词”,
描述:“复数名词”
}];
console.log(Object.values(myQuestions[0].answers).join(',')代码>当您执行以下操作时:
${Object.values(answers).join("")}
然后将join
方法应用于整个answers
对象,并将其键和值连接起来
要仅将值馈送至连接
,请执行以下操作:
answers.join("")
更具体地说:
Object.values(answers).join("")
(如所示)。
const myQuestions=[{
级别:“级别1”,
问题1:,
判决:“她的叔叔是军官。”,
问题:“问:哪些词是关于人的?”,
答复:{
“1”:“a.叔叔/官员”,
“2”:“b.她/你”,
“3”:“c.in/the”
},
正确答案:“1”,
主题:“名词”,
描述:“复数名词”
}]
常量ans=Object.values(myQuestions[0]。答案)
console.log(ans)
什么是答案
?如何从myQuestions[i]创建答案
。答案
?使用Object.values(myQuestions[i].answers)
只获取值。我得到了相同的结果,但您不明白。输出是什么?它和你在你的岗位上得到的一样吗?它起作用了。但是我没有数组。我们能做什么?n个问题或答案的数组?是的,它包含n个问题或答案的数组,但我得到相同的结果这就足够了?
var answers = myQuestions[0].answers;
console.log(Object.values(answers).join(""));