javascript - return statement in forEach won't stop execution of function -


I am trying to determine whether there is a certain object in an array or not. If this happens, I want to capture the function, otherwise it should be added.

  function addPacking (item) {data.packings.forEach (function (entry) {if (item.name == entry.name) {return;}}); Data.packings.push (item); }   

Unfortunately, the data is pushed even when the condition is found. How do I stop this behavior without using the else status?

(I do not want to use else because my actual code is very complex by this and I want to keep it readable)

Edit:

Does for execute asynchronous?

Older ways are sometimes the best, that is because you .forEach passing a delegate function while calling In the representative return is lost, and is not applying to do anything. To get your desired result, you want to exit the calling function addPacking , it can only be done by using for the loop. prefix function addPacking {item} {for (var i = 0; i & lt; data.packings.length ++; i ++) {If (item.name == DataPacking [i ] .name) {return; }} Data.packings.push (item); });

This approach also supports older browsers , on the contrary, and

Comments

Popular posts from this blog

ios - Adding an SKSpriteNode to SKScene from a child SKSpriteNode -

Matlab transpose a table vector -

c# - Textbox not clickable but editable -