简体   繁体   English

如何使用 Javascript 将 HTML 属性添加到数组

[英]How do I add HTML attributes to an array using Javascript

I am trying to add attributes from three HTML elements to an array.我正在尝试将三个 HTML 元素的属性添加到一个数组中。

Here is some sample HTML:下面是一些示例 HTML:

<p x="30">Paragraph 1</p>
<p x="50">Paragraph 2</p>
<p x="100">Paragraph 3</p>

Here is the Javascript that has gotten me closest to the results I am looking for:这是使我最接近我正在寻找的结果的 Javascript:

const paragraphs = document.querySelectorAll(`p`);
      let i;
      for (i = 0; i < paragraphs.length; i++) {
        let dataSetOne = paragraphs[i].getAttribute('x');
        let dataSetOneArray = Array.from(dataSetOne);

      }

When I console.log(dataSetOne), I get "30" "50" "100", which is what I hoped for.当我 console.log(dataSetOne) 时,我得到“30”“50”“100”,这是我所希望的。

When I try to add these attributes to an array using Array.from(dataSetOne), I end up with three arrays that look like this: Array(1) [ "3", "0" ];当我尝试使用 Array.from(dataSetOne) 将这些属性添加到数组时,我最终得到三个如下所示的数组: Array(1) [ "3", "0" ]; Array(2) [ "5", "0" ] Array(3) [ "1", "0", "0" ].数组(2) [ "5", "0" ] 数组(3) [ "1", "0", "0" ]。

The array I am looking for is this: Array ["30", "50", "100"]我正在寻找的数组是这样的:数组 ["30", "50", "100"]

I have tried a few other functions, but none of these are getting me any closer to the result I am looking for.我尝试了其他一些功能,但这些功能都没有让我更接近我正在寻找的结果。

This is not for a specific project.这不是针对特定项目的。 I am trying to advance my understanding of arrays and object-oriented programming.我正在努力提高我对数组和面向对象编程的理解。

Thanks in advance for your help.在此先感谢您的帮助。

Array.from()

The Array.from() method creates a new, shallow-copied Array instance from an array-like or iterable object. Array.from()方法从类数组或可迭代对象创建一个新的、浅复制的 Array 实例。

Since you are using Array.from() inside of the for loop in each iteration new array is creating.由于您在每次迭代的 for 循环内使用Array.from() ,因此正在创建新数组。 You can simply declare the array ( dataSetOneArray ) outside of the loop and push the attribute value directly.您可以简单地在循环之外声明数组 ( dataSetOneArray ) 并直接推送属性值。

const paragraphs = document.querySelectorAll(`p`);
let dataSetOneArray = [];
let i;
for (i = 0; i < paragraphs.length; i++) {
  let dataSetOne = paragraphs[i].getAttribute('x');
  dataSetOneArray.push(dataSetOne);
}

Though, you can achieve that in a more simpler way using Array.prototype.map() :不过,您可以使用Array.prototype.map()以更简单的方式实现:

The map() method creates a new array populated with the results of calling a provided function on every element in the calling array. map()方法创建一个新数组,其中填充了对调用数组中的每个元素调用提供的函数的结果。

 var elList = Array.from(document.querySelectorAll('p')); var res = elList.map(el => el.getAttribute('x')); console.log(res);
 <px="30">Paragraph 1</p> <px="50">Paragraph 2</p> <px="100">Paragraph 3</p>

How about怎么样

let dataSetOneArray = [];
for (i = 0; i < paragraphs.length; i++) {
    let dataSetOne = paragraphs[i].getAttribute('x');
    dataSetOneArray.push(dataSetOne);
  }

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

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