0

Array.find() problems

Hello,

I was going over the provided documentation for Yahoo Connected TV and was trying to find a way to see if - 'a string contained part of a word or a substring'. For example in java you can do something like:

if x.contains("hello"){
....}

I found this example in the documentation that closely mirrors the scenario described above:

var emails = ['test@company.com', 'test2@company.com', 'company.com'];
var reg = new RegExp("@");
var matches = Array.find(reg, emails);
if (matches.length == emails.length){
log("valid email addresses");
} else {
log("invalid email addresses");
}


I tried running this example above - but I get the following error:
WE 00:01:03:371: [T:8985] ReferenceError: KKONtx_automation_log is not defined (helpers.js: Line 535)

Could you please tell me what is wrong?

Also, how do I separate data that is fetched from a URL by using a delimiter? Is there some 'mechanism' in the documentation that describes this? My goal is to fetch some data via a url, parse the relevant pieces of data into an array (based on some substrings), and then use this data in my widget.

Thanks a lot.

by
13 Replies
  • Hi Gurminder,

    QUOTE (Gurminder @ Mar 29 2010, 08:41 PM) <{POST_SNAPBACK}>
    I tried running this example above - but I get the following error:
    WE 00:01:03:371: [T:8985] ReferenceError: KKONtx_automation_log is not defined (helpers.js: Line 535)

    Could you please tell me what is wrong?

    You've found a bug. That logging method is mispelled. For now, if you want to test that Array class method you could comment out the offending line. It's in src/core/helpers.js. Just grep for KKONtx. Unfortunately, that won't be fixed until we patch the framework.

    QUOTE (Gurminder @ Mar 29 2010, 08:41 PM) <{POST_SNAPBACK}>
    Also, how do I separate data that is fetched from a URL by using a delimiter? Is there some 'mechanism' in the documentation that describes this? My goal is to fetch some data via a url, parse the relevant pieces of data into an array (based on some substrings), and then use this data in my widget.

    Do you mean a csv format?

    var response = "one,two,three";
    var myArray = response.split(",");
    myArray.length === 3;

    - Ben
    0
  • QUOTE (Benjamin Toll @ Mar 29 2010, 09:41 PM) <{POST_SNAPBACK}>
    Hi Gurminder,


    You've found a bug. That logging method is mispelled. For now, if you want to test that Array class method you could comment out the offending line. It's in src/core/helpers.js. Just grep for KKONtx. Unfortunately, that won't be fixed until we patch the framework.


    Do you mean a csv format?

    var response = "one,two,three";
    var myArray = response.split(",");
    myArray.length === 3;

    - Ben


    Hi Ben,

    Thank you for the reply, I fixed the logging error.

    With regards to the second part of the post, I understand the delimiter bit, but I think I accidentally asked the wrong question. What I meant to ask was - is there a ".contains()" function (like in java) that lets me do the following:

    if( fetchedData.contains("http://") ){
    ...
    }

    After I find a 'match' i need to find the 'index' of the matching phrase and extract information from it.

    Thanks,
    Zubin
    0
  • QUOTE (Gurminder @ Mar 29 2010, 10:46 PM) <{POST_SNAPBACK}>
    With regards to the second part of the post, I understand the delimiter bit, but I think I accidentally asked the wrong question. What I meant to ask was - is there a ".contains()" function (like in java) that lets me do the following:

    if( fetchedData.contains("http://") ){
    ...
    }

    After I find a 'match' i need to find the 'index' of the matching phrase and extract information from it.

    Hi Zubin,

    How about this?

    CODE
    var str = "http://one,two,http://three";
    var a = str.split(",").filter(function (v, i) {
    return v.indexOf("http://") > -1;
    });


    - Ben
    0
  • QUOTE (Benjamin Toll @ Mar 30 2010, 11:17 AM) <{POST_SNAPBACK}>
    Hi Zubin,

    How about this?

    CODE
    var str = "http://one,two,http://three";
    var a = str.split(",").filter(function (v, i) {
    return v.indexOf("http://") > -1;
    });


    - Ben


    Hi Ben,

    Here is some sample data that I pulled off my web-server by doing the following:
    var url = new URL();
    url.location = "http://***.***.***:****/spring-portlet-sample/PCServices?action=isalertpresent&userid=.....";
    contents = url.fetch();


    So the contents variable holds the following data:
    <?xml version='1.0' encoding='ISO-8859-1'?>
    <response>
    <alert>
    <imagename>2403101233242454_iPhone_9.jpg</imagename>
    <imageurl>***.***.***:****/spring-portlet-sample/ImageServer?action=viewPhoto%26imageemail=*****@gmail.com%26imagename=2403101233242454_iPhone_9.jpg%26emailAddress=gsj23@yahoo.com</imageurl>
    <sgname>670</sgname>
    <sender>BLAH BLAH</sender>
    <upclienttype>BLAH BLAH</upclienttype>
    </alert>

    <alert>
    <imagename>2403103242344_iPhone_10.jpg</imagename>
    <imageurl>***.***.***:****/spring-portlet-sample/ImageServer?action=viewPhoto%26imageemail=*****@gmail.com%26imagename=2403103242344_iPhone_9.jpg%26emailAddress=*****@gmail.com</imageurl>
    <sgname>670</sgname>
    <sender>BLAH BLAH</sender>
    <upclienttype>BLAH BLAH</upclienttype>
    </alert>

    .....

    Sorry for not being clear in my previous posts, but i need to extract the URL that is contained within the <imageurl> ********</imageurl> tags and put that into a data-structure of some sort.

    So this is what I attempted to do (but it doesn't quite work):

    var e = contents.split("<imageurl>").filter(function (v, i) {
    return v.substring("http://", "</imageurl>") > -1;
    });

    So the logic here is that I split the data that I received in my "contents" variable and then I attempt to extract out the URL that I need, and store that.

    This doesn't quite seem to be working for me, the 'rooms' of my array are not getting populated. Please let me know what is going on.

    Thanks for all the help.
    0
  • Since it is XML, you should be able to use XPATH.

    CODE
    urls = doc.evaluate( "/response/imageurl" );
    0
  • Steve's right, use xpath. Check out the xml dom api part of the docs to understand how to extract the data from your nodes.

    Also, I noticed that you're using URL.fetch instead of the asynchronous URL.fetchAysnc. Please be aware that all of your requests must be asynchronous or our QA will bounce back your widget.

    - Ben
    0
  • QUOTE (Benjamin Toll @ Mar 31 2010, 08:46 AM) <{POST_SNAPBACK}>
    Steve's right, use xpath. Check out the xml dom api part of the docs to understand how to extract the data from your nodes.

    Also, I noticed that you're using URL.fetch instead of the asynchronous URL.fetchAysnc. Please be aware that all of your requests must be asynchronous or our QA will bounce back your widget.

    - Ben


    Hello Steve & Ben,

    Thanks for the help.
    I did as you said and looked at the documentation and read some forum posts. I am having a bit of a problem displaying the 'parsed' xml. Here is my code. Please tell me what I am doing wrong. (I added comments for my own understanding). Also a sample XML file can be found above - in this posts thread.

    var request = new XMLHttpRequest();
    var url = "http://***.***.***.***:****/spring-portlet-sample/PCServices?action=isalertpresent&userid=*****@yahoo.com&passwd=******&ct=roku";
    request.open( "GET", url, false );
    request.setRequestHeader( "Content-type", "text/xml" );
    request.send();
    if ( request.status == 200 )
    print( request.responseText ); //object XMLHTTP

    //The bolded line above outputs the XML file

    var myXML = XMLDOM.parse( request.responseXML); //parse XML in DOMDocument
    var element = myXML.evaluate( "string( response/imageurl )" ); //trying to extract all image url's
    print("---- "+element.responseXML); //Object DOMDocument

    //The print statement above does not do anything, the output I get is "----Undefined".


    Thanks for the help.
    0
  • Hi,

    I have been trying out various things and cannot get my code to run. Please see post above - if someone can help out, that would be greatly appreciated.

    Thanks,
    Zubin

    QUOTE (Gurminder @ Mar 31 2010, 02:59 PM) <{POST_SNAPBACK}>
    Hello Steve & Ben,

    Thanks for the help.
    I did as you said and looked at the documentation and read some forum posts. I am having a bit of a problem displaying the 'parsed' xml. Here is my code. Please tell me what I am doing wrong. (I added comments for my own understanding). Also a sample XML file can be found above - in this posts thread.

    var request = new XMLHttpRequest();
    var url = "http://***.***.***.***:****/spring-portlet-sample/PCServices?action=isalertpresent&userid=*****@yahoo.com&passwd=******&ct=roku";
    request.open( "GET", url, false );
    request.setRequestHeader( "Content-type", "text/xml" );
    request.send();
    if ( request.status == 200 )
    print( request.responseText ); //object XMLHTTP

    //The bolded line above outputs the XML file

    var myXML = XMLDOM.parse( request.responseXML); //parse XML in DOMDocument
    var element = myXML.evaluate( "string( response/imageurl )" ); //trying to extract all image url's
    print("---- "+element.responseXML); //Object DOMDocument

    //The print statement above does not do anything, the output I get is "----Undefined".


    Thanks for the help.
    0
  • Just chiming in here, but I think that you need to look into use Async calls as noted by Ben.

    It looks as if you are checking the status, as you would for an async, but you are not calling it correctly. I have not checked it against anything, but these first thoughts looking at the code.
    0
  • QUOTE (Gurminder @ Apr 2 2010, 05:46 PM) <{POST_SNAPBACK}>
    I have been trying out various things and cannot get my code to run. Please see post above - if someone can help out, that would be greatly appreciated.

    Hi Zubin,
    Sorry, it's been pretty busy around here.

    As far as your code is concerned, you're confusing some things.

    You're still doing a synchronous request, which again won't pass QA.
    CODE
    request.open( "GET", url, false );


    Also, you don't need to set this header, as the server is the authority. Just make sure the server's mime type is text/xml.
    CODE
    request.setRequestHeader( "Content-type", "text/xml" );


    The next code snippet has a number of issues:
    CODE
    if ( request.status == 200 )
    print( request.responseText ); //object XMLHTTP

    //The bolded line above outputs the XML file

    var myXML = XMLDOM.parse( request.responseXML); //parse XML in DOMDocument
    var element = myXML.evaluate( "string( response/imageurl )" ); //trying to extract all image url's
    print("---- "+element.responseXML); //Object DOMDocument


    1. You should only be checking the status property inside the function that you would set the onreadystatechange handler to (check out the XMLHttpRequest in the docs) when doing an asynchronous request.
    2. You're also checking both the responseText and the responseXML properties of the object. In your case, you just should check the responseXML property.
    3. Since the value of the responseXML property should already be a document, you don't need to pass it to the XMLDOM.parse method.
    4. The result of calling the evaluate method will be a string, number, or a set of nodes. It will not have a responseXML property. Check out p.289 of the docs.

    Hope this helps. This should get you pointed in the right direction. It seems to me like you just need to get your head around some of the things that you're doing, but you'll get there.

    Also, don't forget the internet is your friend. Do a search for some resources on ajax like this http://www.w3schools.com/ajax/default.asp.

    - Ben
    0
  • QUOTE (Benjamin Toll @ Apr 5 2010, 11:56 AM) <{POST_SNAPBACK}>
    Hi Zubin,
    Sorry, it's been pretty busy around here.

    As far as your code is concerned, you're confusing some things.

    You're still doing a synchronous request, which again won't pass QA.
    CODE
    request.open( "GET", url, false );


    Also, you don't need to set this header, as the server is the authority. Just make sure the server's mime type is text/xml.
    CODE
    request.setRequestHeader( "Content-type", "text/xml" );


    The next code snippet has a number of issues:
    CODE
    if ( request.status == 200 )
    print( request.responseText ); //object XMLHTTP

    //The bolded line above outputs the XML file

    var myXML = XMLDOM.parse( request.responseXML); //parse XML in DOMDocument
    var element = myXML.evaluate( "string( response/imageurl )" ); //trying to extract all image url's
    print("---- "+element.responseXML); //Object DOMDocument


    1. You should only be checking the status property inside the function that you would set the onreadystatechange handler to (check out the XMLHttpRequest in the docs) when doing an asynchronous request.
    2. You're also checking both the responseText and the responseXML properties of the object. In your case, you just should check the responseXML property.
    3. Since the value of the responseXML property should already be a document, you don't need to pass it to the XMLDOM.parse method.
    4. The result of calling the evaluate method will be a string, number, or a set of nodes. It will not have a responseXML property. Check out p.289 of the docs.

    Hope this helps. This should get you pointed in the right direction. It seems to me like you just need to get your head around some of the things that you're doing, but you'll get there.

    Also, don't forget the internet is your friend. Do a search for some resources on ajax like this http://www.w3schools.com/ajax/default.asp.

    - Ben



    Hello Ben,

    Thanks so much for the pointers and the link to the resource ( I am fairly new to this stuff).
    So I went through the AJAX resource and the documentation, and understand things a little bit better.

    First question I have is with regards to point 3). you said that since the value of the 'response' should already be an XML-document and that i do not need to pass it to the XMLDOM.parse method.

    But supposing I do not parse the response-Data that results from the response to the URL, how do i reference the "evaluate" method (when I do this.evaluate(....) from within the "myStatusProc()" method , I get an error saying "this.evaluate" is not a method)?
    I understand that since I am attempting to open a XML document, this "parsing" step is not necessary.
    The only way I could make this work was by doing:
    CODE
    var myXML = XMLDOM.parse( this.responseData);
    var element = myXML[b].evaluate[/b]( "string( response/imageurl )" );


    Also from looking at p.289 of the guide and the AJAX tutorial I want to print out the contents of an element from the XML -> "imageurl"
    just to see if I able to 'grab' requested data. I attempted to try this by doing:
    CODE
    var testFetchingData = myXML.getElementById("imageurl");


    Here is the complete re-factored code containing the asynchronous call and the "myStatusProc".

    CODE
                                           var request = new XMLHttpRequest();
    var url = "http://***.***.**.***:****/spring-portlet-sample/PCServices?action=isalertpresent&userid=**&passwd=**&ct=roku";
    request.onreadystatechange = myStatusProc;
    request.open( "GET", url, true ); //last parameter determines if call is asynchronous
    request.send();

    // "this" refers to the request
    function myStatusProc()
    {

    if ( this.readyState == 4 ) // complete
    {
    if (this.status == 200)
    {
    print( "THE STATUS:" + this.status );
    var myXML = XMLDOM.parse( this.responseData);
    var element = myXML.evaluate( "string( response/imageurl )" ); //Trying to extract the "imageurl" from XML
    var testFetchingData = myXML.getElementById("imageurl"); //Printing out some test data

    }
    }
    }

    (the sample output of XML file structure is in previous thread posts)

    Appreciate the help!

    -Zubin
    0
  • Also I tried the method of not parsing the this.responseData and went straight to do the following:

    CODE
     var element = this.responseXML.evaluate( "string( response/imageurl )" );


    I get an error that says:
    this.responseXML has no properties

    So it seems that this.responseXML is not holding any data. I might just be screwing up my syntax, but im searching online on what I am doing wrong.

    Thanks,
    Zubin
    0
  • Zubin,
    Hopefully, this is what you're looking to do, or if not, you could modify this to suit your needs.

    First, here's the xml I'm consuming, based on your earlier post:
    CODE
    <?xml version='1.0' encoding='ISO-8859-1'?>
    <response>
    <alert>
    <imagename>2403101233242454_iPhone_9.jpg</imagename>
    <imageurl>***.***.***:****/spring-portlet-sample/ImageServer?action=viewPhoto%26imageemail=*****@gmail.com%26imagename=2403101233242454_iPhone_9.jpg%26emailAddress=gsj23@yahoo.com</imageurl>
    <sgname>670</sgname>
    <sender>BLAH BLAH</sender>
    <upclienttype>BLAH BLAH</upclienttype>
    </alert>

    <alert>
    <imagename>2403103242344_iPhone_10.jpg</imagename>
    <imageurl>http://***.***.***:****/spring-portlet-sample/ImageServer?action=viewPhoto%26imageemail=*****@gmail.com%26imagename=2403103242344_iPhone_9.jpg%26emailAddress=*****@gmail.com</imageurl>
    <sgname>670</sgname>
    <sender>BLAH BLAH</sender>
    <upclienttype>BLAH BLAH</upclienttype>
    </alert>

    <alert>
    <imagename>2403103242344_iPhone_10.jpg</imagename>
    <imageurl>***.***.***:****/spring-portlet-sample/ImageServer?action=viewPhoto%26imageemail=*****@gmail.com%26imagename=2403103242344_iPhone_9.jpg%26emailAddress=*****@gmail.com</imageurl>
    <sgname>670</sgname>
    <sender>BLAH BLAH</sender>
    <upclienttype>BLAH BLAH</upclienttype>
    </alert>

    <alert>
    <imagename>2403103242344_iPhone_10.jpg</imagename>
    <imageurl>http://***.***.***:****/spring-portlet-sample/ImageServer?action=viewPhoto%26imageemail=*****@gmail.com%26imagename=2403103242344_iPhone_9.jpg%26emailAddress=*****@gmail.com</imageurl>
    <sgname>670</sgname>
    <sender>BLAH BLAH</sender>
    <upclienttype>BLAH BLAH</upclienttype>
    </alert>
    </response>

    Next, here's how I parse the xml document and extract what I need from the nodes, i.e., the image urls that contain 'http://':
    CODE
                    var xhr = new XMLHttpRequest();
    xhr.open("GET", "http:/www.example.com/zubin.xml", true);
    xhr.onreadystatechange = function () {
    if (xhr.readyState === 4) {
    if (xhr.status === 200) {
    var xml = xhr.responseXML,
    oImages = xml.evaluate("//imageurl)"),
    urls = [];

    for (var i = 0, len = oImages.length; i < len; i++) {
    var url = oImages.item(i).firstChild.data;
    if (url.indexOf("http://") !== -1) {
    urls.push(url);
    }
    }
    }
    }
    };
    xhr.send(null);

    Make sense?

    - Ben
    0

Recent Posts

in General - Yahoo! TV Widgets