javascript - Vanilla JS - ES6: .map() is not a function - Stack Overflow

I am trying to refactor my for loop with .map() and ES6 and I am getting an error message stating&quo

I am trying to refactor my for loop with .map() and ES6 and I am getting an error message stating ".map is not a function"

//basic tab function
function openContent(tabpages) {
  var page = document.getElementsByClassName("content");

  // for (i = 0; i < page.length; i++) {
  //   page[i].style.display = "none";
  // }
  page.map(page => (page.style.display = "none"));

  document.getElementById(tabpages).style.display = "flex";
}

I am trying to refactor my for loop with .map() and ES6 and I am getting an error message stating ".map is not a function"

//basic tab function
function openContent(tabpages) {
  var page = document.getElementsByClassName("content");

  // for (i = 0; i < page.length; i++) {
  //   page[i].style.display = "none";
  // }
  page.map(page => (page.style.display = "none"));

  document.getElementById(tabpages).style.display = "flex";
}

Please point me to the right direction.

Share Improve this question edited Nov 10, 2018 at 8:48 Felix Kling 818k181 gold badges1.1k silver badges1.2k bronze badges asked Nov 10, 2018 at 8:26 criscris 2251 gold badge6 silver badges12 bronze badges 0
Add a ment  | 

3 Answers 3

Reset to default 3

page is an HTMLCollection which is an Object not an Array. You can extract the object's keys to an array for mapping using Object.keys()

For example:

//basic tab function
function openContent(tabpages) {
  var page = document.getElementsByClassName("content");

  // for (i = 0; i < page.length; i++) {
  //   page[i].style.display = "none";
  // }
  Object.keys(page).map(idx => (page[idx].style.display = "none"));

  document.getElementById(tabpages).style.display = "flex";
}

As already mentioned, getElementsByClassName doesn't return an array, so you cannot call .map on it.

The simplest way to convert to an array would be to use Array.from:

var page = Array.from(document.getElementsByClassName("content"));

However, .map is not the right tool for what you are doing. .map creates a new array from the return values of the callback. But you don't actually intend to use that array.

You simply want to iterate over all elements. You can do this easily without converting the node list to an array, using for...of:

for (var page of document.getElementsByClassName("content")) {
  page.style.display = "none";
}

Given that you're iterating over the elements, and not — from the code you've posted — doing anything with the Array returned from Array.prototype.map() Id suggest using document.querySelectorAll() and NodeList.prototype.forEach() to iterate over that NodeList:

document.querySelectorAll('.content').forEach( (page) => page.style.display = 'none' );

Although I'd also remend using a class to hide the elements, rather than modifying the inline-styles directly:

document.querySelectorAll('.content').forEach( (page) => page.classList.add('hidden') );

Obviously that does require a CSS definition for the hidden class-name.

References:

  • document.querySelectorAll().
  • Element.classList API.
  • NodeList.prototype.forEach().

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744830282a4596041.html

相关推荐

  • javascript - Vanilla JS - ES6: .map() is not a function - Stack Overflow

    I am trying to refactor my for loop with .map() and ES6 and I am getting an error message stating&quo

    2天前
    50

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信