2

I get the array of xml nodes using jquery with the following code

var appointments = new Array();
appointments = $($myXML).find('confirmationNumber');

Which returns me:

[
  <confirmationNumber>​NX0FH25P​</confirmationNumber>​, 
  <confirmationNumber>​VX0MW251</confirmationNumber>​, 
  <confirmationNumber>​VB0TH252​</confirmationNumber>​,
  <confirmationNumber>​VB0MH253</confirmationNumber>​
]

I want to retrieve the value of the following confirmation numbers as texts in array and I dont want to iterate the whole XML

I tried:

appointment[i].text() and appointment[i].val(); 

but that didn't work.

1 Answer 1

2

You can use map() to project the text of the elements into a new array without explicitly looping:

var confirmationNumbers = $($myXML).find("confirmationNumber").map(function() {
    return $(this).text();
}).get();
Sign up to request clarification or add additional context in comments.

3 Comments

but there is one problem. it is not just returning the text. it is actually returning the array of strings of the value. is there any way to get individual strings ?
@Faizan, yes, you requested an array in your question, so... What do you mean by individual strings? Did you try indexing the array (e.g. confirmationNumber[0])?
I solved the problem by casting it to string. using.toString() like confirmationNumber[0].toString(). actually before it was returning array of strings like ["n","x","0","F","H","2","5","P"], and so on...

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.