此变量是一个数组.为什么它返回未定义
This variable is an array. Why is it returning undefined?
这是解决第4章"列表"中Eloquent Javascript挑战的部分尝试。数组返回undefined,但如果我只是打印它的值,它就是我所期望的(值的数组)。为什么它返回undefined?
var obj = {"value":"C","rest":{"value":"B","rest":{"value":"A"}}};
var theArray =[];
var listToArray = function(list) {
theArray.push(list.value);
if(list.rest !== undefined) {
listToArray(list.rest);
} else return theArray; //console.log(theArray); returns the expected value
}
console.log(listToArray(obj));
您必须返回递归调用
if(list.rest !== undefined) {
return listToArray(list.rest);
演示
如果不返回递归调用,则不会返回来自最终函数堆栈的array
,而是返回undefined
。
相关文章:
- 为什么数组在对象内部迭代对象时存储重复值
- 当我添加命名属性时,为什么数组会有这种行为
- 为什么数组上的 js 映射会修改原始数组
- 为什么数组的大小不是 7
- 为什么数组和对象([ ] + {})的求和是javascript中的一个字符串
- 为什么数组在删除后仍然具有非零长度
- 为什么数组中的字符串索引不会增加'长度'
- 为什么数组中的数字有效,而数组中的字符串无效
- 为什么数组不在javascript中按递增日期或递减日期排序
- 为什么”——数组.删除数组的最后一个元素
- 为什么数组不显示在文本区域中?
- 为什么数组'的长度属性'的值改变,即使在javascript中设置为只读
- 为什么数组推不工作在我的功能
- 为什么数组失去了它的值
- 为什么数组上的. tostring给出一个奇怪的结果
- 为什么数组未定义
- 不能弄清楚为什么数组在不应该更新的时候会更新
- 为什么数组在$localStorage工作
- 为什么数组.prototype是一个数组
- 为什么数组未定义