1

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
7
  • Try, console.log(key, arrTerms, Array.isArray(arrTerms)); Commented Apr 1, 2015 at 15:59
  • The code as it is works fine for me (except your missing a comma in your array definition). What does results actually look like? Commented Apr 1, 2015 at 15:59
  • It prints typeOf as Object Commented Apr 1, 2015 at 15:59
  • @ZeMoon Sorry, try Array.isArray. I edited the comment Commented Apr 1, 2015 at 16:02
  • @thefourtheye Array.isArray(arrTerms) returns true Commented Apr 1, 2015 at 16:03

1 Answer 1

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
        });
     });
    }
});
Sign up to request clarification or add additional context in comments.

8 Comments

So, what happens to attTerms if results[key] is undefined?
Just nothing. :) We don't want to do anything if we can't find the key, do we?
But you are still calling arrTerms.forEach even if results[key] is undefined.
Oh, overseen that :) ty
@ZeMoon: _.keys, just like Object.keys, will get all property names of the object. So if you have {foo: undefined}, then you get ['foo']. So it seems, after all, you didn't post the actual structure of the object :P I wonder why didn't catch that, since you called console.log(keys); already.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.