Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/421.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 为什么要转向二维阵列?_Javascript_Arrays_Object - Fatal编程技术网

Javascript 为什么要转向二维阵列?

Javascript 为什么要转向二维阵列?,javascript,arrays,object,Javascript,Arrays,Object,我这里有一些关于对象的模拟问题(不要介意这些问题和选择,它们都是胡言乱语) 现在我要做智商测试 let retQuestions = Object.values(questions.iq_test).map(item => { console.log(item); return item; }) 地图中的console.log提供了这种格式 [ { question: 'Find the difference', choices: [ 'a1',

我这里有一些关于对象的模拟问题(不要介意这些问题和选择,它们都是胡言乱语)

现在我要做智商测试

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时,我得到了不同的格式

log(retQuestions),正如您在下面看到的,它变成了2d数组

[ [ { 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 } ] ]

这一次我想再次映射以重新生成问题以获取每个问题,但我不能,因为格式正在更改

Object.values
返回该对象中所有值的数组。由于
questions.iq_test
本身就是一个只有一个键值对的对象,因此在本例中,您将返回一个2d数组。试着记录一次

对于您试图实现的目标,您应该直接调用对象值上的map。比如说,

let retQuestions = questions.iq_test.questions.map(item => {
      console.log(item);
      return item;
});
方法返回给定对象自身可枚举属性值的数组,其顺序与for…in循环提供的顺序相同(区别在于for in循环也枚举原型链中的属性)

因此,在您的情况下,您需要对
问题.iq\u测试.questions
执行
map
,并且由于您只是返回
,因此可以避免使用
返回
关键字

const问题={
智商测试:{
问题:[
{
问题:“找出差异”,
选项:['a1','a2','a1','a3'],
正确:0
}, 
{
问题:'下一个序列是什么',
选择:['2','5','56','64'],
正确:3
}, 
{问题:'下一个字母应该是什么',
选项:['c','z','e','f'],
正确:3
}]
},
记忆测试:{
}
}
让retQuestions=questions.iq_test.questions.map(item=>item);

控制台日志(返回问题)
由于使用
map
您需要
retQuestions.forEach(x=>console.log(x))
@appleapple OP不需要
console.log
值:)@AnkitAgarwal:)
let retQuestions = questions.iq_test.questions.map(item => {
      console.log(item);
      return item;
});