[英]Taking all values from an array in the same command
In order to take a specific value from an array element I use this: 为了从数组元素中获取特定的值,我使用以下命令:
document.querySelectorAll('li[dat*="era"] ul li.title div')[0].textContent
if I want to take the next value I have to replace the 0 with 1 etc. 如果要使用下一个值,则必须将0替换为1等。
Is it possible to use the same command and take all the values from the array using something like this: 是否可以使用相同的命令,并使用类似以下方法从数组中获取所有值:
document.querySelectorAll('li[dat*="era"] ul li.title div')[0:40].textContent
You can use the Array.map()
function: 您可以使用Array.map()
函数:
document.querySelectorAll('li[dat*="era"] ul li.title div').map(x => x.textContent);
EDIT 编辑
If your environment does not support arrow functions, you can use map with a plain function too: 如果您的环境不支持箭头功能,则也可以将map与普通功能一起使用:
document.querySelectorAll('li[dat*="era"] ul li.title div').map(function(x) {
return x.textContent
});
Use Array.from() 使用Array.from()
The Array.from() method creates a new Array instance from an array-like or iterable object. Array.from()方法从类似于数组或可迭代对象的对象创建一个新的Array实例。
with Array.prototype.forEach() 与Array.prototype.forEach()
The forEach() method executes a provided function once for each array element. forEach()方法为每个数组元素执行一次提供的函数。
Array.from(document.querySelectorAll('li[dat*="era"] ul li.title div')).forEach(function (currentValue, index, array) {
// your code
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.