简体   繁体   English

尝试在 JavaScript 中将 for 循环更改为单行过滤器方法

[英]Trying to change a for loop to a filter method one-liner in JavaScript

I have been trying to change a for loop to filter() one-liner, but I get this error: (index):44 Uncaught TypeError: childrenElements.filter is not a function .我一直在尝试将for loop更改for loop filter() one-liner,但出现此错误: (index):44 Uncaught TypeError: childrenElements.filter is not a function

Working code snippet:工作代码片段:

const parentElement = document.querySelector('.container');

let childrenElements = parentElement.children;

let results = [];

for(i = 0; i < childrenElements.length; i++){
  if( childrenElements[i].classList.contains('ui-widget')){
    results.push('ui-widget')
  }
}

Not Working code snippet:不工作的代码片段:

const parentElement = document.querySelector('.container');

let childrenElements = parentElement.children;

let results = [];

results = childrenElements.filter(childrenElement => childrenElement.classList.contains('ui-widget') )

I think it's because it doesn't recognize the childrenElements as array , but I'm not sure how to fix it.我认为这是因为它无法将childrenElements识别为array ,但我不确定如何修复它。

You can find a demo here .您可以在此处找到演示

EDIT:编辑:

The accepted's answer final code snippet is the following:接受的答案最终代码片段如下:


const parentElement = document.querySelector('.container');

let childrenElements = parentElement.children;

let results = [];

results = [...childrenElements].map(childrenElement => childrenElement.classList );

console.log(results)

if (results[0].contains('ui-widget')) {
    alert('it has class')
}

You can convert your HTMLCollection object to an array using the spread operator, so that your data is iterable:您可以使用扩展运算符将 HTMLCollection 对象转换为数组,以便您的数据可迭代:

results = [...childrenElements].filter(childrenElement => childrenElement.classList );

However, it seems like you are trying to look for a particular class in the elements.但是,您似乎正在尝试在元素中查找特定类。 For that you should map over your elements to create an array of classList arrays and then use contains to search for the element you're looking for.为此,您应该映射元素以创建一个 classList 数组,然后使用contains来搜索您要查找的元素。

results = [...childrenElements].map(childrenElement => childrenElement.classList );

if (results[0].contains('ui-widget')) {
    alert('it has class');
}

The reason for that is that childrenElements is a HTMLCollection that is enumerable, which means you can access its objects using indexes .原因是 childrenElements 是一个可枚举的HTMLCollection ,这意味着您可以使用索引访问它的对象。 Eg childrenElements[0]例如childrenElements[0]

However, it does not provide the same methods provided by the Array object.然而,它提供通过所提供的相同的方法Array对象。 Therefore, you will need to convert it to an Array by using:因此,您需要使用以下方法将其转换为数组:

Array.from(childrenElements)

Then, you can use all of the methods provided for an Array .然后,您可以使用为Array提供的所有方法。

Please be aware that if your list is too big, this might not be the most performant way as you are converting your HTMLCollection to an Array and then, looping all over again on it to filter elements.请注意,如果您的列表太大,这可能不是最高效的方式,因为您将 HTMLCollection 转换为一个数组,然后在其上再次循环以过滤元素。

In your code childrenElements is HTMLCollection which is array like object not an array.在您的代码中 childrenElements 是 HTMLCollection,它是像对象一样的数组而不是数组。 So, you just need to convert that to array and it will work.所以,你只需要将它转换为数组,它就会起作用。

 const parentElement = document.querySelector('.container'); let childrenElements = parentElement.children; console.log(childrenElements) let results = []; // working code snippet - uncomment to test it /* for(i = 0; i < childrenElements.length; i++){ if( childrenElements[i].classList.contains('ui-widget')){ results.push('ui-widget') } } console.log(results) */ // not working code snippet results = Array.from(childrenElements).filter(childrenElement => childrenElement.classList ) console.log(results) if (results[0] === 'ui-widget') { alert('it has class') }
 <div class="container"> <div class="ui-widget ui-widget-content widget-left"> I am a ui-widget </div> <p class="paragraph"> Hello Paragraph </p> <button class="btn"> Close </button> </div>

使用这个函数把它变成一个数组

var realArray = Array.from(childrenElements);

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

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