将jquery每个函数转换为纯javascript
我有脚本在选择框中显示下拉列表。我当前使用的脚本是将jquery每个函数转换为纯javascript,javascript,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 (dd
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);
});
});
它不起作用。请注意native
forEach
中参数顺序的差异-第一个是项的值,第二个是索引。因此,不是:
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]);
请参见您使用的
.forEach()
方法错误。
不需要将数组作为第一个参数传入。只需通过回拨
dslr.forEach(function(dslrgp) {
// do something..
}
或者使用键/值迭代
dslr.forEach(function(value, index) {
// do something..
}
“它不工作”-定义“不工作”。给出一个清晰的问题陈述。您得到的行为与您期望的行为有何不同?“将jquery每个函数转换为纯javascript”-jquery是纯javascript。有充分的理由想要删除对jQuery的依赖,但不要将jQuery误认为是JavaScript以外的任何东西。我已经编辑了我的问题。这里的arr、currentValue和array是什么?