我越来越anoter的forEach函数内的问题的forEach:错误调用foreach所阵列
的results
变量包含类似于对象:
{
names: [
'Someone',
'Someone else'
],
emails: [
'[email protected]'
'[email protected]'
]
}
我希望它放松所有的阵列和导致数组是这样的:
[
{term: 'Someone', type: 'names'},
...
]
这里是我的代码:
var keys = _.keys(results);
console.log(keys);
var finalResult = [];
keys.forEach(function (key) {
var arrTerms = results[key];
console.log(key, arrTerms); //arrTerms prints fine
arrTerms.forEach(function (term) { //This line throws an exception
finalResult.push({
term: term,
type: key
});
});
});
到的forEach嵌套调用抛出以下异常:
TypeError: Uncaught error: Cannot call method 'forEach' of undefined
我尝试使用for循环与迭代直到长,但它产生的另一个例外:
TypeError: Uncaught error: Cannot read property 'length' of undefined
尝试,'的console.log(键,arrTerms,Array.isArray(arrTerms));' – thefourtheye 2015-04-01 15:59:07
,因为它是我工作正常(除了丢失的阵列中一个逗号的代码定义)。 “结果”究竟是什么样子? – 2015-04-01 15:59:32
它将typeOf打印为对象 – ZeMoon 2015-04-01 15:59:35