Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #33637 > unrolled thread

Function to explode not working... Need some help please.

Started byhazael00@gmail.com
First post2016-02-07 00:32 -0800
Last post2016-02-08 12:38 -0500
Articles 4 — 3 participants

Back to article view | Back to comp.lang.java.programmer


Contents

  Function to explode not working... Need some help please. hazael00@gmail.com - 2016-02-07 00:32 -0800
    Re: Function to explode not working... Need some help please. Arne Vajhøj <arne@vajhoej.dk> - 2016-02-07 12:33 -0500
      Re: Function to explode not working... Need some help please. Eric Douglas <e.d.programmer@gmail.com> - 2016-02-08 05:20 -0800
        Re: Function to explode not working... Need some help please. Arne Vajhøj <arne@vajhoej.dk> - 2016-02-08 12:38 -0500

#33637 — Function to explode not working... Need some help please.

Fromhazael00@gmail.com
Date2016-02-07 00:32 -0800
SubjectFunction to explode not working... Need some help please.
Message-ID<e2f22306-52c2-48b4-974e-4a3ea18bd614@googlegroups.com>
I'm using Kimono to extract some data and create an API:

    {
      "name": "site update",
      "count": 4,
      "frequency": "Manual Crawl",
      "version": 1,
      "newdata": true,
      "lastrunstatus": "success",
      "thisversionstatus": "success",
      "thisversionrun": "Sun Feb 07 2016 05:13:26 GMT+0000 (UTC)",
      "results": {
        "collection1": [
          {
            "title": {
              "href": "http://www.tvtrailers.com/scenes/view/id/9418/title-1/",
              "text": "Title 1"
            },
            "pubDate": "February 6, 2016",
            "index": 1,
            "url": "http://www.tvtrailers.com/home/"
          },
          {
            "title": {
              "href": "http://www.tvtrailers.com/scenes/view/id/9422/title-2/",
              "text": "Title 2"
            },
            "pubDate": "February 6, 2016",
            "index": 2,
            "url": "http://www.tvtrailers.com/home/"
          },
          {
            "title": {
              "href": "http://www.tvtrailers.com/scenes/view/id/9358/title-3/",
              "text": "Title 3"
            },
            "pubDate": "February 5, 2016",
            "index": 3,
            "url": "http://www.tvtrailers.com/home/"
          },
          {
            "title": {
              "href": "http://www.tvtrailers.com/scenes/view/id/9419/title-4/",
              "text": "Title 4"
            },
            "pubDate": "February 5, 2016",
            "index": 4,
            "url": "http://www.tvtrailers.com/home/"
          }
        ]
      }
    }

I'm trying to `GET` the value of `href` inside the `title` `element`, then 
`explode` the `string` to obtain the id number (`9418`, `9422`, `9358`, `9419` on the code above) and create a new property with just the id number. 

Or, if is not possible to create a new property, then I would like to just replace all the `href` string and keep the id number instead of the full `href` url.

Here is the updated `function` I'm using with the code provided by Trincot on the comments below:

   

     // main function
        function addPostNumbers(data) {
            for(var collection in data.results) {
                data.results[collection].forEach(function(row) {
                    if (parts = row.title.href.match(/\/(\d+)\//)) {
                        row.title.post_number = parts[1];
                    }
                });
            }
        }
    
    // call the main function
    addPostNumbers(data);
    
    // write result in document.
    document.write('<pre>' + JSON.stringify(data, null, 2) + '</pre>');


Result:

    {
      "error": "Bad Request",
      "message": "Your function failed to evaluate because of Error: Unexpected identifier"
    }

Kimono Inspector warnings
> line 5: Assignment in conditional expression.<br><br>
> line 8: Don't make functions within a loop.<br><br>
> line 16: document.write can be a form of eval<br><br>

Using only the `addPostNumbers(data)` without the `call` & `write` result

    {
      "error": "Bad Request",
      "message": "Your function failed to evaluate because of Error: parts is not defined"
    }

Also the code inspector inside kimono had 2 warnings:

> On line 7: Don't make functions within a loop.

> On line 8: Unnecessary semicolon

I appreciate any help and directions to figure out what's wrong with the code above, 
thank you.

[toc] | [next] | [standalone]


#33643

FromArne Vajhøj <arne@vajhoej.dk>
Date2016-02-07 12:33 -0500
Message-ID<n97v4j$e36$2@gioia.aioe.org>
In reply to#33637
On 2/7/2016 3:32 AM, hazael00@gmail.com wrote:
> I'm using Kimono to extract some data and create an API:
>
>      {
>        "name": "site update",
>        "count": 4,
>        "frequency": "Manual Crawl",
>        "version": 1,
>        "newdata": true,
>        "lastrunstatus": "success",
>        "thisversionstatus": "success",
>        "thisversionrun": "Sun Feb 07 2016 05:13:26 GMT+0000 (UTC)",
>        "results": {
>          "collection1": [
>            {
>              "title": {
>                "href": "http://www.tvtrailers.com/scenes/view/id/9418/title-1/",
>                "text": "Title 1"
>              },
>              "pubDate": "February 6, 2016",
>              "index": 1,
>              "url": "http://www.tvtrailers.com/home/"
>            },

> I'm trying to `GET` the value of `href` inside the `title` `element`,
> then `explode` the `string` to obtain the id number (`9418`, `9422`,
> `9358`, `9419` on the code above) and create a new property with just
> the id number.
>
> Or, if is not possible to create a new property, then I would like
> tojust replace all the `href` string and keep the id number instead of
 > the full `href` url.
>
> Here is the updated `function` I'm using with the code provided by
> Trincot on the comments below:
>
>       // main function
>          function addPostNumbers(data) {
>              for(var collection in data.results) {
>                  data.results[collection].forEach(function(row) {
>                      if (parts = row.title.href.match(/\/(\d+)\//)) {
>                          row.title.post_number = parts[1];
>                      }
>                  });
>              }
>          }
>
>      // call the main function
>      addPostNumbers(data);
>
>      // write result in document.
>      document.write('<pre>' + JSON.stringify(data, null, 2) + '</pre>');

That does not look like Java.

Arne

[toc] | [prev] | [next] | [standalone]


#33671

FromEric Douglas <e.d.programmer@gmail.com>
Date2016-02-08 05:20 -0800
Message-ID<d7ce6561-16b4-48f5-8c9c-79dd79b33eda@googlegroups.com>
In reply to#33643
On Sunday, February 7, 2016 at 12:33:28 PM UTC-5, Arne Vajhøj wrote:

> That does not look like Java.
> 
> Arne

Javascript?

[toc] | [prev] | [next] | [standalone]


#33680

FromArne Vajhøj <arne@vajhoej.dk>
Date2016-02-08 12:38 -0500
Message-ID<n9ajqc$a8m$2@gioia.aioe.org>
In reply to#33671
On 2/8/2016 8:20 AM, Eric Douglas wrote:
> On Sunday, February 7, 2016 at 12:33:28 PM UTC-5, Arne Vajhøj wrote:
>> That does not look like Java.
>
> Javascript?

Think so.

Arne

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.java.programmer


csiph-web