admin管理员组

文章数量:1323383

The Array.filter() method is having an asynchronous behavior. According to the documentation, it seems to me it's not supposed to work that way.

Moreover,it does not allow the reference to a callback, as usual in such cases.

I am limited in relation to the use of libraries, but also confuses me, how it reacts that way.

I am not also expert in ECMAScript, am i making some mistake?

I just want to filter one array and send the results to a HTML select element as several options.

I have something like this:

var selCompanyDeps = departments.filter(fromSelectedCompany);

fillSelect($("#selDeparts"), selCompanyDeps, 'departmentID', 'name', selectedID);

function fromSelectedCompany(value){
    var selectedCompany = $( "#SelComps" ).val();
    return (valuepanyID===Number(selectedCompany));
}

Thanks

The Array.filter() method is having an asynchronous behavior. According to the documentation, it seems to me it's not supposed to work that way.

Moreover,it does not allow the reference to a callback, as usual in such cases.

I am limited in relation to the use of libraries, but also confuses me, how it reacts that way.

I am not also expert in ECMAScript, am i making some mistake?

I just want to filter one array and send the results to a HTML select element as several options.

I have something like this:

var selCompanyDeps = departments.filter(fromSelectedCompany);

fillSelect($("#selDeparts"), selCompanyDeps, 'departmentID', 'name', selectedID);

function fromSelectedCompany(value){
    var selectedCompany = $( "#SelComps" ).val();
    return (value.panyID===Number(selectedCompany));
}

Thanks

Share Improve this question edited Aug 3, 2015 at 18:50 King 3,0953 gold badges13 silver badges22 bronze badges asked Aug 3, 2015 at 18:06 Pedro GuedesPedro Guedes 1091 silver badge10 bronze badges 5
  • 5 .filter is not an async method. – tymeJV Commented Aug 3, 2015 at 18:08
  • 1 It does not provide a callback because it is not asynchronous. I cannot discern a question here. – Alex Booker Commented Aug 3, 2015 at 18:09
  • What does departments contain? – axelduch Commented Aug 3, 2015 at 18:19
  • Yes, i saw .filter wasn´t an async method tymeJV, and that's my question Petrichor. – Pedro Guedes Commented Aug 4, 2015 at 7:22
  • aduch, departments is a array – Pedro Guedes Commented Aug 4, 2015 at 7:22
Add a ment  | 

1 Answer 1

Reset to default 7

Array.filter is not an asynchronous method, but what you appear to be confusing is the order of execution of a JavaScript programme.

When the browser parses your code, the browser will look for named functions and add them to the list of declared names in the current scope (known as function hoisting).

Therefore, fromSelectedCompany is executed before you call fillSelect, so your code is equivalent to the following:

function fromSelectedCompany(value){
    var selectedCompany = $( "#SelComps" ).val();
    return (value.panyID===Number(selectedCompany));
}

var selCompanyDeps = departments.filter(fromSelectedCompany);

fillSelect($("#selDeparts"), selCompanyDeps, 'departmentID', 'name', selectedID);

本文标签: JavaScript Arrayfilter()Stack Overflow