简体   繁体   中英

How to return an element in a forEach loop

In my code i have an array that is like below:

localStroge_Procrastinator_tasks = [object , object, object]

Now i want to iterate over the array using a forEach loop and return when the id of an object matches the id i provide , let me lay out the code to make my example clear.

storeRelevantObj = localStroge_Procrastinator_tasks.forEach(function(e, i){
    if (e.task_Id === task_Id) {
        return e;
    }
});

But wheni run the code even if the if conditon passes and e is returned , storeRelevantObj still remains empty , why ? why is the return statement not working ??

Don't use forEach() for filtering, use filter() instead:

var storeRelevantObj = localStroge_Procrastinator_tasks.filter(function(e) {
     return e.task_Id === task_Id;
})[0];

If you don't mind IE support, you can use the find() method that is returning the first element that satisfies the condition (it's also more performant since it can return immediately upon first match):

var storeRelevantObj = localStroge_Procrastinator_tasks.find(function(e) {
     return e.task_Id === task_Id;
});

Also, if you're targeting ES6 or using a transpiler (like Babel ), your predicate can be expressed using an Arrow Function :

var storeRelevantObj = localStroge_Procrastinator_tasks.find(e => e.task_Id === task_Id);

Use Array#filter instead of Array#forEach

With Array#forEach , empty array could be initialized and do Array#push if condition is met!

Using Array#filter , new array with all elements that pass the test is returned!

var storeRelevantObj = localStroge_Procrastinator_tasks.filter(function(e, i) {
  return e.task_Id === task_Id;
});

You need to use a filter function here. Because you want to return an array. The forEach you are currently using will keep overwriting the value of storeRelevantObj.

Code:

storeRelevantObj = localStroge_Procrastinator_tasks.filter((e, i)=>{ return (e.task_Id === task_Id); });

If you need to use forEach, you should do this:

localStroge_Procrastinator_tasks.forEach((e, i)=>{ if (e.task_Id === task_Id){ storeRelevantObj.push(e)
}; });

Using Array#filter , will return a new array . you can use also use Array#reduce .
var storeRelevantObj = localStroge_Procrastinator_tasks.reduce(function(prev, cur) { return prev || cur.task_id == task_id; }, false);

However this will only return the first matched value. While filter will return an array with all matched values

The following documentation states that there is no way out of a forEach other than throwing an exception. It also mentions what your options are if you need such behavior!

You can also use a simple for loop the following way:

localStroge_Procrastinator_tasks = [object , object, object];

var i, l, match;


l =  localStroge_Procrastinator_tasks.length;
for(i=0;!match&&i<l;++i){
    match = localStroge_Procrastinator_tasks[i].task_Id === task_Id;
}

if match is set to true inside the loop then next evaluation will not proceed!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM