[英]convert jquery each function to pure javascript
我有脚本在选择框中显示下拉列表。 我当前使用的脚本是
jQuery.each( dslr, function( index, dslrgp) {
var aslrp= dslrgp.aslrp;
jQuery.each( aslrp, function(index2, pslrp) {
var found = 0;
jQuery.each( dropdown, function(index3, dditem) {
if (dditem.countryname == pslrp.countryname)
{
foundit = 1;
}
});
if (foundit == 0)
dropdown.push(pslrp);
});
});
我如何将其转换为纯JavaScript。 因为如果我用这个
dslr.forEach(function( index, dslrgp) {
var aslrp= dslrgp.aslrp;
aslrp.forEach(function(index2, pslrp) {
var found = 0;
dropdown.forEach(function(index3, dditem) {
if (dditem.countryname == pslrp.countryname)
{
foundit = 1;
}
});
if (foundit == 0)
dropdown.push(pslrp);
});
});
它不起作用。
请注意,本机forEach
中参数顺序的差异-第一个是item的值,第二个是index。 所以代替:
aslrp.forEach(function(index2, pslrp) {
...
dropdown.forEach(function(index3, dditem) {
用这个:
aslrp.forEach(function(pslrp, index2) {
...
dropdown.forEach(function(dditem,index3) {
您的方法签名错误。 它的:
arr.forEach(function callback(currentValue, index, array) {
//your iterator
}[, thisArg]);
参见https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach
您使用的.forEach()
方法错误。 forEach文档
您不需要将数组作为第一个参数传入。 只需传递回调即可。
dslr.forEach(function(dslrgp) {
// do something..
}
或键/值迭代
dslr.forEach(function(value, index) {
// do something..
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.