Why Does This Foreach Return Undefined When Using a Return Statement

forEach function returns undefined for some reason

The main reason you get undefined is because you are returning from forEach and not from itemExist() function.

The way you would implement the following case using forEach is as follows:

let items = ['item-1','item-2','item-3','item-4'];

function itemExist(itemList, itemName) {
let isItemPresent = false;
itemList.forEach( (item) => {
if(item === itemName) {
isItemPresent = true;
return;
}
})
if(isItemPresent) {
return true;
} else {
return false;
}
}

console.log(itemExist(items, 'item-3')) // expected output: true

console.log(itemExist(items, 'item-5')) // expected output: false

Function with forEach returns undefined even with return statement

In your function, you're returning from the function passed to forEach, not from getByKey.

You could adapt it like this :

function getByKey(key) {    
var found = null;
data.forEach(function (val) {
if (val.Key === key) {
found = val;
}
});
return found;
}

But this would iterate over all elements, even if the item is immediately found. That's why you'd better use a simple for loop :

function getByKey(key) {    
for (var i=0; i<data.length; i++) {
if (data[i].Key === key) {
return data[i];
}
}
}

Note that I also adapted your code to return the value, not the key. I suppose that was the intent. You might also have been confused with another iteration function : the first argument passed to the callback you give to forEach is the element of the array.

Why does this forEach return undefined when using a return statement

The function e() isn't returning anything; the inner anonymous function is returning its e value but that return value is being ignored by the caller (the caller being function e() (and can the multiple uses of 'e' get any more confusing?))

Why is .forEach returning undefined?

forEach doesn't return anything. It just loops through elements and while looping you can change element data

And so you can change your function SOtest to

function SOtest () {
testArray.forEach(test => {
test.newSegments = test.segments ? testsMap[test.segments] : [];
test.display = helperFn(); // will add true/false to the test prop

if (test.display) {
test.tests.map(t => {
t.newSegments = t.segments ? testsMap[t.segments] : [];
t.display = helperFn(); // will add true/false to the test prop
})
}
})
return testArray;
}

Why is my foreach and map return undefined

ForEach method works in such way in which it doesn't return anything while it works. So even if you add return statement in your forEach, it wouldn't return anything. In your case you can change forEach to map method which returns new array

forEach returns undefined - How to return an array instead?

.forEach returns undefined. You probably want to .map to a new array and return that:

 return nums.map(function (element) {
return element * ((element % 2 == 0) ? 2 : 3);
});

For sure you also have to return the statement inside of the inner function, otherwise that evaluates to undefined too.

forEach/for...in not returning values?

As the other answers explain, this is meaningless:

collection.forEach(function () {
// do something
return false;
});

because array#forEach simply does not care for the return value of its worker function. It just executes the worker function for each array element.

You could use the worker function to set an outer variable:

function truthCheck(collection, pre) {
var allAreTruthy = true;
collection.forEach(function (elem) {
// if this ever flips allAreTruthy to false, it will stay false
allAreTruthy = allAreTruthy && elem[pre];
});
return allAreTruthy;
}

But there are better ways to express this.

Check if the predicate (second argument) is truthy on all elements of a collection (first argument).

Could be paraphrased as "Every element of the collection has a truthy value at a particular key."

function truthCheck(collection, pre) {
return collection.every(function (elem) { return elem[pre]; });
}

Could be paraphrased as "None of the elements of the collection have a falsy value at a particular key (or are missing the key entirely)."

Or, since an Array#none method does not actually exist, "There aren't some elements of the collection that have a falsy value at a particular key."

function truthCheck(collection, pre) {
return !collection.some(function (elem) { return !elem[pre]; });
}

The advantage of using Array#some is that it stops iterating the array as soon as the condition it seeks for is fulfilled. If your array had many elements this would mean improved performance. For short arrays there's not a lot of difference to using Array#every or Array#forEach.

The above is semantically equivalent to

function truthCheck(collection, pre) {
var i;
for (i = 0; i < collection.length; i++) {
if (!collection[i][pre]) return false;
}
return true;
}

Since JS objects simply return undefined when you access a key that has not been set, a check for hasOwnProperty is superfluous here.



Related Topics



Leave a reply



Submit