Joe Berg
Joe Berg

Reputation: 381

Store array list in variable

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);
}

Upvotes: 0

Views: 1576

Answers (2)

jsadev.net
jsadev.net

Reputation: 2590

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);
}

Upvotes: 1

Steven Spungin
Steven Spungin

Reputation: 29071

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>

Upvotes: 1

Related Questions