29

How can I modify this code so that I can grab the field.DependencyFieldEvaluated value and get out of the function as soon I get this value?

function discoverDependentFields(fields) {
                    fields.forEach(function (field) {
                        if (field.DependencyField) {
                            var foundFields = fields.filter(function (fieldToFind) { return fieldToFind.Name === field.DependencyField; });
                            if (foundFields.length === 1) {
                               return field.DependencyFieldEvaluated = foundFields[0];

                            }
                        }
                    });
                }
Maryam
  • 1,417
  • 4
  • 19
  • 33
  • whats wrong with function above ?? – egig Feb 17 '16 at 06:45
  • 1
    Why not use `for` loop instead of `forEach` function? – tdragon Feb 17 '16 at 06:46
  • 2
    you can't break out of forEach(), try using `every` instead http://stackoverflow.com/questions/6260756/how-to-stop-javascript-foreach – gurvinder372 Feb 17 '16 at 06:46
  • firstly it complains that not all path returns a value. secondly it continues on the loop even when it reaches the return line! – Maryam Feb 17 '16 at 06:47
  • 2
    @Maryam - the .forEach() function is *supposed to* continue the loop - it ignores the return value from your function. If you use .every() instead you can break out early but *only by returning false* - that is to say that you can't directly return a value to the code outside the loop, you have to assign the value to a variable declared outside. If what you really want to do is return that value from the `discoverDependentFields()` function then the easiest way is just to use a traditional `for` loop. – nnnnnn Feb 17 '16 at 06:57
  • I tried for loop. The problem is that in the nested function then it cannot access fields[i]. any solution to this? – Maryam Feb 17 '16 at 07:14
  • Please share a sample json you are using to call the function. – zakaiter Feb 17 '16 at 07:16

3 Answers3

27

Use a good old vanilla for loop:

function discoverDependentFields(fields) {
  for (var fieldIndex = 0; fieldIndex < fields.length; fieldIndex ++) {
    var field = fields[fieldIndex];

    if (field.DependencyField) {
      var foundFields = fields.filter(function(fieldToFind) {
        return fieldToFind.Name === field.DependencyField;
      });
      if (foundFields.length === 1) {
        return foundFields[0];
      }
    }
  }
}

Well, if you want to stay fancy, use filter again:

function discoverDependentFields(fields) {
  return fields.filter(function(field) {
    if (field.DependencyField) {
      var foundFields = fields.filter(function(fieldToFind) {
        return fieldToFind.Name === field.DependencyField;
      });
      if (foundFields.length === 1) {
        return foundFields[0];
      }
    }
  })[0];
}
lxe
  • 7,051
  • 2
  • 20
  • 32
10

instead of fields.forEach you can use fields.map. here is an example:

var source=[1,2,3,4,5];

var destination=source.map(function(item){
 if(item==3)
     return 'OUTPUT';
}).filter(function(item){return item;})[0];

console.log(destination); // prints 'OUTPUT'
Barkermn01
  • 6,781
  • 33
  • 83
behzad besharati
  • 5,873
  • 3
  • 18
  • 22
-11

Use return statement where you want to break-out like below.

[1, 2, 3, 4, 5, 6].forEach(function(value){
  if(value > 2) return; 
  console.log(value)
});
Ravi Tiwari
  • 946
  • 8
  • 17
  • Break in what respect. The callback that you pass in forEach executes for each item of the data and hence can't have a single return value. However, if you want not to process the callback for specific condition, you can use return. If you could provide the problem at hand, not the code, I guess I would be able to help, not sure though :) – Ravi Tiwari Feb 17 '16 at 07:17
  • A return statement inside a .forEach() callback behaves like a `continue`, not like a `break`. But in any case if you look at the code in the question you'll see the OP is already trying to use `return`. – nnnnnn Feb 17 '16 at 10:03