0

I have a $.each cycle and I need to access an external variable and I have troubles managing it. ex:

var f = obj instanceof QuadTable ? _.find(obj.tableCols, {data: arr[2]}) : $("#" + frm + ' input[name=' + arr[0] + ']');
 $.each(arrTags, function (i, item) {
        var arr=$(item).data('filter').split(":")
        var y = obj instanceof QuadTable ? _.find(obj.tableCols, {data: $.trim(arr[0])}) : $("#" + frm + ' input[name=' + $.trim(arr[0]) + ']');
        if (y.complexList || (y instanceof $ && y.hasClass("complexList"))) {
                var keys = obj.returnListKeys(y);
                var values = [];
                debugger;
                _.map(keys, function (name, i) {
                    debugger;
                    var k = _.findIndex(obj.dbColumns, {'db': name});
                    values = arr[1].split('@');
                    obj.dbColumns[k]["prv_value"] = values[i];
                    obj.dbColumns[k]["nxt_value"] = values[i];
                });
        }
    });

My problem is how to access f inside $.each.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Leonel Matias Domingos
  • 1,922
  • 5
  • 29
  • 53
  • Just like any other variable. – Ele Jan 02 '18 at 12:40
  • 1
    You should be able to access `f` with your current logic, assuming it's not coming from an async function call – Rory McCrossan Jan 02 '18 at 12:40
  • Useful reading: [*How do JavaScript closures work?*](https://stackoverflow.com/questions/111102/how-do-javascript-closures-work) and [*JavaScript closure inside loops - simple practical example*](https://stackoverflow.com/questions/750486/javascript-closure-inside-loops-simple-practical-example) (but in your case, `f` is an invariant across the entire loop, so that second is more just info for the future). – T.J. Crowder Jan 02 '18 at 12:42
  • i so sorry i'll remove the question....tired – Leonel Matias Domingos Jan 02 '18 at 12:43
  • you can directly access f in a $.each function. – Negi Rox Jan 02 '18 at 12:48

0 Answers0