【发布时间】:2018-05-10 13:37:14
【问题描述】:
我在这里有一些关于对象的模拟问题(不要介意他们胡言乱语的问题和选择)
const questions = {
iq_test: {
questions: [
{
question: 'Find the difference',
choices: ['a1', 'a2', 'a1', 'a3'],
correct: 0
},
{
question: 'What is the next sequence',
choices: ['2', '5', '56', '64'],
correct: 3
},
{ question: 'What should be the next letter',
choices: ['c', 'z', 'e', 'f'],
correct: 3
}]
},
memory_test: {
...omitted
}
}
现在我想要iq_test
let retQuestions = Object.values(questions.iq_test).map(item => {
console.log(item);
return item;
})
地图中的 console.log 给了我这种格式
[ { question: 'Find the difference',
choices: [ 'a1', 'a2', 'a1', 'a3' ],
correct: 0 },
{ question: 'What is the next sequence',
choices: [ '2', '5', '56', '64' ],
correct: 3 },
{ question: 'What should be the next letter',
choices: [ 'c', 'z', 'e', 'f' ],
correct: 3 } ]
但是当我 console.log 应该保存返回值的 retQuestions 我得到不同的格式
console.log(retQuestions) 如下所示,它变成了二维数组
[ [ { question: 'Find the difference', choices: [Array], correct: 0 },
{ question: 'What is the next sequence',
choices: [Array],
correct: 3 },
{ question: 'What should be the next letter',
choices: [Array],
correct: 3 } ] ]
这次我想再次映射到 retQuestions 以获取每个问题,但我不能,因为格式正在改变..
【问题讨论】:
-
因为你使用
map你需要retQuestions.forEach(x=>console.log(x)) -
@appleapple OP 只是不需要
console.log的值:) -
@AnkitAgarwal :)
标签: javascript arrays object