3 回答
![?](http://img1.sycdn.imooc.com/545845b40001de9902200220-100-100.jpg)
TA贡献1810条经验 获得超4个赞
有多个问题
假设对象有一个变量 obj
var loop = () => {
var arr = []
for (var i = 0 ; i< obj.messages.length; i ++) { //messages is a key of an object, so messages is undefined, it should be obj.messages.
arr.push(messages[i]) //wrong index, you should push `i` and not 1
}
return arr; // loop() is a function, causing endless recursion, causing stack overflow!
console.log(arr) // will never print since function already returns!; move before return if you want it to print
}
![?](http://img1.sycdn.imooc.com/545862db00017f3402200220-100-100.jpg)
TA贡献1784条经验 获得超2个赞
你可以使用解构,它简短而简单。
var obj = { "messages": [
{
"msgFrom": "13223821242",
"msgBody": "Hi there"
}, {
"msgFrom": "Bill",
"msgBody": "Hello!"
}
]
};
var arr = [...obj.messages];
console.log(arr);
添加回答
举报