Function with Foreach Returns Undefined Even with Return Statement

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;
}

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.

forEach is returning undefined

The return value of Array.prototype.forEach() is undefined, you can not return anything explicitly from forEach().

You can try with Array.prototype.map() instead:

The map() method creates a new array populated with the results of calling a provided function on every element in the calling array.

function checkCars(carData) {
const cars = carData.models;

return models.map(model => {
if (model.type === 'Toyota' || model.type === 'Hyundai') {
return "Type A";
} else {
return "Type B";
}
});
}

Why does this function returns undefined?

The reason you get undefined is because the function isThree() doesn't return anything. The two returns in your function return from the anonymous function inside foreach().

I think your function tries to determine if there's a value 3 in the array. There is already a function for that: includes(). Your function could be:

function isThree(...args) {
return args.includes(3);
};

console.log(isThree(1,2,3,4,5));

Why is this javascript function returning undefined?

That return statement you have in there doesn't return a value to the outer function, it only returns a value to the inner function called by forEach.



Related Topics



Leave a reply



Submit