0

I am trying to store the array list in a variable but have no clue how to solve it.

The xpath will return a list of items, and it is displayed in console just fine. My issue is that I have no idea how I could store it in a variable to use the list later.

If I try to use "arrayList" it will only return the last item from the array, but in console it displays all items.

Any ideas?

var iterator = document.evaluate('xpathgoeshere', document, null, XPathResult.UNORDERED_NODE_ITERATOR_TYPE, null);

try {
  var thisNode = iterator.iterateNext();

  while (thisNode) {
    var arrayList = (thisNode.textContent);
    var thisNode = iterator.iterateNext();
    for (var i = 0; i < arrayList.length; i++) {
      console.log(arrayList);
    }
  }
} catch (e) {
  dump('Error: Document tree modified during iteration ' + e);
}
3
  • What is arrayEpisodes, and why are you not using the index arrayList[i] in your loop? Commented Dec 3, 2018 at 11:53
  • @StevenSpungin Sorry, forgot to update the name, it is arrayList as well. If I use arrayList[i] it returns the same result for me. Do you know why my variable only returns the last result? Commented Dec 3, 2018 at 13:02
  • textContent is a string, so not sure why you are iterating it. Also, try to put var thisNode = iterator.iterateNext(); at the end of the while. If you want to store the items, create an array and stash them. I will add a snippet to solution. Commented Dec 3, 2018 at 13:32

2 Answers 2

1

at first you should update thisNode after your loop, becaus arrayList is facing thisNode.

The next issue could be, that you're setting iterator.iterateNext(); to a new var thisNode inside your while-loop, instead of updating your var thisNode inside your try-block. (because of the var infront of it)

Try this: :)

var iterator = document.evaluate('xpathgoeshere', document, null, XPathResult.UNORDERED_NODE_ITERATOR_TYPE, null);

try {
  var thisNode = iterator.iterateNext();

  while (thisNode) {
    var arrayList = thisNode.textContent;

    for (var i = 0; i < arrayList.length; i++) {
      console.log(arrayList[i]);
    }    
    thisNode = iterator.iterateNext();
  }
} catch (e) {
  dump('Error: Document tree modified during iteration ' + e);
}

As Steven mentioned, thisNode.textContent is a string. I dont know how your sting looks like, but maybe you have to split() it first, or if its a JSONstring you have to use JSON.parse(), to get your Array.


But, if your thisNode.textContexts should be the items of your Array, try this:

var iterator = document.evaluate('xpathgoeshere', document, null, XPathResult.UNORDERED_NODE_ITERATOR_TYPE, null);

try {
  var thisNode = iterator.iterateNext();
  var arrayList = [];

  while (thisNode) {
    arrayList.push(thisNode.textContent); 
    thisNode = iterator.iterateNext();
  }

  console.log(arrayList);

  for (var i = 0; i < arrayList.length; i++) {
    console.log(arrayList[i]);
  }   
} catch (e) {
  dump('Error: Document tree modified during iteration ' + e);
}
Sign up to request clarification or add additional context in comments.

1 Comment

Yes the second one is correct, the items are from thisNode.textContext. And thank you so much J. Sadi, works like a charm! :)
1

Add your nodes to an array.

var iterator = document.evaluate('//div', document, null, XPathResult.UNORDERED_NODE_ITERATOR_TYPE, null);

const items = []

try {
  var thisNode = iterator.iterateNext();
  while (thisNode) {
    items.push(thisNode)
    var thisNode = iterator.iterateNext();
  }
} catch (e) {
  dump('Error: Document tree modified during iteration ' + e);
}

console.log(items);
<div>
  <div>Item 1</div>
  <div>Item 2</div>
</div>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.