0%

js querySelectorAll 到数组

querySelectorAll 结果 是 nodelist 不是数组!!!!

var a = Array.from(document.querySelectorAll(‘label’));

var a = […document.querySelectorAll(‘label’)];

var a = Array.prototype.map.call(document.querySelectorAll(‘label’),k=>k);

var a = Array.prototype.slice.call(document.querySelectorAll(‘label’))

var a = Array.prototype.filter.call(document.querySelectorAll(‘label’),k=>k);

var a = [];for(i of document.querySelectorAll(‘label’)){a.push(i);}

var a = [], rs = document.querySelectorAll(‘label’);for(i=0;i< rs.length;i++){a.push(rs[i]);}

var a = [], rs = document.querySelectorAll(‘label’);rs.forEach(k=>k);

var a = [];document.querySelectorAll(‘label’).forEach(k=>a.push(k));

var a = [], rs = document.querySelectorAll(‘label’);for(k of rs.entries()){a.push(k);}

var a = [], rs = document.querySelectorAll(‘label’);for(k of rs.entries()){a.push(k[1]);}

var a = [], rs = document.querySelectorAll(‘label’);for(k of rs.values()){a.push(k);}

var a =[];
var rs = document.querySelectorAll(‘label’);

some 只要1个符合;every 全部符合 返回 true

var a = [];
Array.prototype.some.call(document.querySelectorAll(‘label’),k=>{
a.push(k);
});

var a = [];
Array.prototype.every.call(document.querySelectorAll(‘label’),k=>{
return a.push(k);
});

一直找不到。。。

var a = [];
Array.prototype.find.call(document.querySelectorAll(‘label’),k=>{
return !a.push(k);
});

https://medium.com/poka-techblog/simplify-your-javascript-use-map-reduce-and-filter-bd02c593cc2d

https://medium.com/mr-frontend-community/5-ways-to-loop-over-dom-elements-from-queryselectorall-in-javascript-55bd66ca4128

https://medium.com/@chuckdries/traversing-the-dom-with-filter-map-and-arrow-functions-1417d326d2bc

Array keys

for (var elem of Array(3).keys()){
console.log(elem);
}

// If you really want to use forEach…
[…Array(3).keys()].forEach(k => console.log(‘spread syntax -> ‘, k));

// Or using Array.from
Array.from(Array(3).keys()).forEach(k => console.log(‘Array.from ->’, k));