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) returns true – ZeMoon Commented Apr 1, 2015 at 16:03
 |  Show 2 more ments

1 Answer 1

Reset to default 1

I 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) returns true – ZeMoon Commented Apr 1, 2015 at 16:03
 |  Show 2 more ments

1 Answer 1

Reset to default 1

I 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