Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

Welcome to Software Development on Codidact!

Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.

Comments on How to make this treewalker code having a regular for loop or a forEach() method?

Parent

How to make this treewalker code having a regular for loop or a forEach() method?

+0
−3

Credit for User:Meriton for developing the following code (first published here).

function replaceIn(e) {
  if (e.nodeType == Node.TEXT_NODE) {
    e.nodeValue = e.nodeValue.replaceAll("a", "");
  } else {
    for (const child of e.childNodes) {
      replaceIn(child);
    }
  }
}

replaceIn(document.body);

I don't want to use a for...of loop here by principle, because I personally think its syntax is confusing.

How to make this tree walker having a regular for loop instead, or, alternatively, a forEach() method instead?

History
Why does this post require moderator attention?
You might want to add some details to your flag.
Why should this post be closed?

0 comment threads

Post
+4
−0

I personally think its syntax is confusing.

Well, it's just a matter of getting used to it, I guess :-)


Anyway, this code is just looping through all childNodes and in each iteration it calls the replaceIn function, passing the element as argument.

Therefore, you could replace the for..of with a traditional for loop:

function replaceIn(e) {
  if (e.nodeType == Node.TEXT_NODE) {
    e.nodeValue = e.nodeValue.replaceAll("a", "");
  } else {
    for (let i = 0; i < e.childNodes.length; i++) {
      replaceIn(e.childNodes[i]);
    }
  }
}

Or you could use the forEach method:

function replaceIn(e) {
  if (e.nodeType == Node.TEXT_NODE) {
    e.nodeValue = e.nodeValue.replaceAll("a", "");
  } else {
    e.childNodes.forEach(replaceIn);
  }
}

With both, you achieve the same result.

History
Why does this post require moderator attention?
You might want to add some details to your flag.

2 comment threads

A question (6 comments)
Comments (6 comments)
Comments
deleted user wrote almost 2 years ago · edited almost 2 years ago

Believe it or not, I still misunderstand what's actually being done here.

Because all I want is to just replace a character in a very specific element, I may have some psychological block in my mind to understand anything which seems to me "unconnected" for that task.

I imagine, perhaps wildly, just a 1-3 line command that will do that.

In my mind, perhaps even the following should have been enough:

if (e.nodeType == Node.TEXT_NODE) {
  e.nodeValue = e.nodeValue.replaceAll("a", "");
}

Anyway, here:

function replaceIn(e) {
  if (e.nodeType == Node.TEXT_NODE) {
    e.nodeValue = e.nodeValue.replaceAll("a", "");
  } else {
    e.childNodes.forEach(replaceIn);
  }
}

I don't understand where should I declare what is the element that I work on.

I mean I don't understand where document.querySelector('#myElement'); should come.

hkotsubo‭ wrote almost 2 years ago · edited almost 2 years ago

deleted user You simply call the function with the element: replaceIn(document.querySelector('whatever')). Or, as it was made in your other question: replaceIn(document.body).

There's no replaceAll because the elements in a page are organized in a DOM-tree - it's a hierarchical structure (as explained here, each part of the page will be in a different node). Calling replace in one node just changes that node, but you need to traverse through all the other nodes to accomplish the task. That's what the for and the recursive call are doing: you get the child nodes, and for each one you call the function again (and that will replace that node, call the function in its child nodes, which will replace, call the function in its child nodes, and so on, until all nodes are replaced)

hkotsubo‭ wrote almost 2 years ago · edited almost 2 years ago

Consider the analogy of the labyrinth in Dirk's answer: to do the task in all rooms you need to visit all of them. There's no way to do that without actually visiting every single room.

In a similar way, you can't replace all the nodes without actually visiting every single node. That's why you need to call the function in every child node (therefore, you need the for loop, it doesn't matter if it's a for..of, forEach, etc).

deleted user wrote almost 2 years ago

hkotsubo‭

Yes I know the DOM is hierarchal.

Here is something I have ran in the console and didn't work:

function replaceIn(e) {
  if (e.nodeType == Node.TEXT_NODE) {
    e.nodeValue = e.nodeValue.replaceAll("א", "");
  } else {
    e.childNodes.forEach(replaceIn(document.body));
  }
}

undefined

Words with א remain the same.

hkotsubo‭ wrote almost 2 years ago · edited almost 2 years ago

e.childNodes.forEach(replaceIn(document.body)) is wrong, the correct is e.childNodes.forEach(replaceIn) - that's because the forEach method receives the function that will be called for each element. By doing replaceIn(document.body), you're calling the function, and the result is being passed to forEach

Skipping 1 deleted comment.

deleted user wrote almost 2 years ago · edited almost 2 years ago

hkotsubo‭

I understand now

I missed the function call.

When I ran the code you developed as follows, it worked, all א's were vanished:

function replaceIn(e) {
    if (e.nodeType == Node.TEXT_NODE) {
        e.nodeValue = e.nodeValue.replaceAll("א", "");
    } else {
        e.childNodes.forEach(replaceIn);
    }
}

replaceIn(document.body);

I do need to better understand what's actually being done and now, at least, thanks to your help, I have the shortest and simplest example to study.

I really like if-else conditionals but in this case for some reason it gives me hard time to understand what is going on.