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
console.log(key, arrTerms, Array.isArray(arrTerms));resultsactually look like?Array.isArray. I edited the commentArray.isArray(arrTerms)returnstrue