admin管理员组文章数量:1023083
I am getting a problem forEach within anoter forEach function:
The results
variable contains an object like:
{
names: [
'Someone',
'Someone else'
],
emails: [
'[email protected]'
'[email protected]'
]
}
I want it to unwind all the arrays and result in an array like this:
[
{term: 'Someone', type: 'names'},
...
]
Here is my code:
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
});
});
});
The nested call to forEach throws the following exception:
TypeError: Uncaught error: Cannot call method 'forEach' of undefined
I tried using a for loop with iteration till length, but it generated another exception:
TypeError: Uncaught error: Cannot read property 'length' of undefined
I am getting a problem forEach within anoter forEach function:
The results
variable contains an object like:
{
names: [
'Someone',
'Someone else'
],
emails: [
'[email protected]'
'[email protected]'
]
}
I want it to unwind all the arrays and result in an array like this:
[
{term: 'Someone', type: 'names'},
...
]
Here is my code:
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
});
});
});
The nested call to forEach throws the following exception:
TypeError: Uncaught error: Cannot call method 'forEach' of undefined
I tried using a for loop with iteration till length, but it generated another exception:
TypeError: Uncaught error: Cannot read property 'length' of undefined
Share
Improve this question
asked Apr 1, 2015 at 15:55
ZeMoonZeMoon
20.3k5 gold badges60 silver badges99 bronze badges
7
-
Try,
console.log(key, arrTerms, Array.isArray(arrTerms));
– thefourtheye Commented Apr 1, 2015 at 15:59 -
The code as it is works fine for me (except your missing a ma in your array definition). What does
results
actually look like? – Explosion Pills Commented Apr 1, 2015 at 15:59 - It prints typeOf as Object – ZeMoon Commented Apr 1, 2015 at 15:59
-
@ZeMoon Sorry, try
Array.isArray
. I edited the ment – thefourtheye Commented Apr 1, 2015 at 16:02 -
@thefourtheye
Array.isArray(arrTerms)
returnstrue
– ZeMoon Commented Apr 1, 2015 at 16:03
1 Answer
Reset to default 1I think the problem here is that you may assign undefined to your arrTerms (when results[key] returns undefined cause you take a key which isn't contained in your object). Try to do this:
var keys = _.keys(results);
console.log(keys);
var finalResult = [];
keys.forEach( function (key) {
if(results[key] != undefined){
var arrTerms = results[key];
arrTerms.forEach(function (term) { //This line throws an exception
console.log(key, arrTerms); //arrTerms prints fine
finalResult.push({
term: term,
type: key
});
});
}
});
I am getting a problem forEach within anoter forEach function:
The results
variable contains an object like:
{
names: [
'Someone',
'Someone else'
],
emails: [
'[email protected]'
'[email protected]'
]
}
I want it to unwind all the arrays and result in an array like this:
[
{term: 'Someone', type: 'names'},
...
]
Here is my code:
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
});
});
});
The nested call to forEach throws the following exception:
TypeError: Uncaught error: Cannot call method 'forEach' of undefined
I tried using a for loop with iteration till length, but it generated another exception:
TypeError: Uncaught error: Cannot read property 'length' of undefined
I am getting a problem forEach within anoter forEach function:
The results
variable contains an object like:
{
names: [
'Someone',
'Someone else'
],
emails: [
'[email protected]'
'[email protected]'
]
}
I want it to unwind all the arrays and result in an array like this:
[
{term: 'Someone', type: 'names'},
...
]
Here is my code:
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
});
});
});
The nested call to forEach throws the following exception:
TypeError: Uncaught error: Cannot call method 'forEach' of undefined
I tried using a for loop with iteration till length, but it generated another exception:
TypeError: Uncaught error: Cannot read property 'length' of undefined
Share
Improve this question
asked Apr 1, 2015 at 15:55
ZeMoonZeMoon
20.3k5 gold badges60 silver badges99 bronze badges
7
-
Try,
console.log(key, arrTerms, Array.isArray(arrTerms));
– thefourtheye Commented Apr 1, 2015 at 15:59 -
The code as it is works fine for me (except your missing a ma in your array definition). What does
results
actually look like? – Explosion Pills Commented Apr 1, 2015 at 15:59 - It prints typeOf as Object – ZeMoon Commented Apr 1, 2015 at 15:59
-
@ZeMoon Sorry, try
Array.isArray
. I edited the ment – thefourtheye Commented Apr 1, 2015 at 16:02 -
@thefourtheye
Array.isArray(arrTerms)
returnstrue
– ZeMoon Commented Apr 1, 2015 at 16:03
1 Answer
Reset to default 1I think the problem here is that you may assign undefined to your arrTerms (when results[key] returns undefined cause you take a key which isn't contained in your object). Try to do this:
var keys = _.keys(results);
console.log(keys);
var finalResult = [];
keys.forEach( function (key) {
if(results[key] != undefined){
var arrTerms = results[key];
arrTerms.forEach(function (term) { //This line throws an exception
console.log(key, arrTerms); //arrTerms prints fine
finalResult.push({
term: term,
type: key
});
});
}
});
本文标签: javascriptError calling forEach on arrayStack Overflow
版权声明:本文标题:javascript - Error calling forEach on array - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745571667a2156773.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论