admin管理员组文章数量:1323560
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
1 Answer
Reset to default 7Array.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
版权声明:本文标题:JavaScript Array.filter() - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742118114a2421564.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论