简体   繁体   English

如何从此 NodeList 中提取数据?

[英]How do I extract data from this NodeList?

I need to extract a particular data property from a NodeList .我需要从NodeList提取特定的数据属性。 I have no problems getting data from arrays or objects in general, but this NodeList is beyond me!一般来说,我从数组或对象获取数据没有问题,但是这个NodeList超出了我的范围! It doesn't work even if I use Array.from() to turn it into an array.即使我使用Array.from()将其转换为数组,它也不起作用。

This is the relevant code:这是相关代码:

在此处输入图片说明

And this is what it logs:这是它记录的内容:

在此处输入图片说明

In the log on line 173, the closed arrays contain all the data I need, but I simply don't understand how to go there.在第 173 行的日志中,封闭数组包含我需要的所有数据,但我根本不明白如何去那里。 When I try to go to an index there it just opens up the path coordinates.当我尝试转到那里的索引时,它只会打开路径坐标。

I will also add the code image as text, it doesn't have any lines though:我还将代码图像添加为文本,但它没有任何行:

let test = d3.selectAll(".unit")
    console.log(test)
    console.log(test._groups)
    console.log(test._groups[0])
    console.log(test._groups[0][0])

EDIT: To be more specific, the data I need is a " data " property inside the array below the nodelist (?), compare to the previous image of the log on line 173:编辑:更具体地说,我需要的数据是节点列表(?)下方数组内的“数据”属性,与第 173 行日志的前一个图像进行比较: 在此处输入图片说明

EDIT2: To be even more clear: When I open the nodelist in the console, I also get an array, and it is only the array that interests me. EDIT2:更清楚一点:当我在控制台中打开节点列表时,我也会得到一个数组,而且只有我感兴趣的数组。 I don't understand this data structure, how the array is related to the nodelist, but I have tried to access the indexes of the array in a variety of ways and nothing has worked.我不明白这个数据结构,数组如何与节点列表相关,但我尝试以各种方式访问​​数组的索引,但没有任何效果。

NodeList objects are collections of nodes. NodeList对象是节点的集合。 In some cases, the NodeList is live , which means that changes in the DOM automatically update the collection.在某些情况下, NodeListlive ,这意味着 DOM 中的更改会自动更新集合。 In other cases, the NodeList is static , where any changes in the DOM does not affect the content of the collection.在其他情况下, NodeListstatic ,其中 DOM 中的任何更改都不会影响集合的内容。 The document.querySelectorAll() method returns a static NodeList , for instance.例如, document.querySelectorAll()方法返回一个静态NodeList

It's possible to loop over the items in a NodeList using a for loop:可以使用 for 循环遍历 NodeList 中的项目:

for (let i = 0; i < myNodeList.length; i++) {
  let item = myNodeList[i];
}

for...of loops will loop over NodeList objects correctly: for...of循环将正确循环 NodeList 对象:

const list = document.querySelectorAll('input[type=checkbox]');
for (let checkbox of list) {
  checkbox.checked = true;
}

You can use item() method to get an item by its index.您可以使用item()方法通过索引获取项目。 For instance, this is how you can get id of the first <div> on some page:例如,您可以通过以下方式获取某个页面上第一个<div> id

document.querySelectorAll("div").item(0).id

In your case, you have an array and it contains an element of type NodeList .在您的情况下,您有一个数组,它包含一个NodeList类型的元素。 So, when you do test._groups[0] you get the first element of your array and this element is NodeList and you need to work with it as with NodeList (see above)!因此,当您执行test._groups[0]您将获得数组的第一个元素,该元素是NodeList ,您需要像使用 NodeList 一样使用它(见上文)! For instance:例如:

const arr = [1,2]; // simple array
// arr[0] is 1.
// in your case you have an array, but elements in that array are of type NodeList 
// (that's why console shows something like [NodeList(...)])

// when you get something from your array - it will be NodeList
// hence you can iterate over it or get some particular item like
test._groups[0].item(0).ariaAtomic

There are a lot more useful methods.还有很多有用的方法。 Check docs for more details.查看文档以获取更多详细信息。

To extrat data from nodeList you must loop your node list and the stock nodList elemnts in otheer tab要从 nodeList 中提取数据,您必须在其他选项卡中循环您的节点列表和股票 nodList 元素

var list = node.childNodes; 

// Using for..of 
for(var value of list.values()) { 
  console.log(value); 
}

[check this link https://developer.mozilla.org/en-US/docs/Web/API/NodeList/values][1]

NodeList objects are collections of nodes. NodeList 对象是节点的集合。

You can loop through a node list by using the NodeList.length property and read the innerHTML of it as follows.您可以使用NodeList.length属性循环遍历节点列表,并按如下方式读取其 innerHTML。

And refer the document for more knowlege并参考文档以获取更多知识

var items = document.getElementsByTagName("p");

var gross = "";
for (var i = 0; i < items.length; i++) {
   gross += items[i].innerHTML;
}

and also you can loop through the你也可以循环

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM