javascript - Kimono: Function to modify data, explode string and add new a new property -


i'm using kimono extract data , create 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 get value of href inside title element, explode string obtain id number (9418, 9422, 9358, 9419 on code above) , create new property id number.

or, if not possible create new property, replace href string , keep id number instead of full href url.

here code i'm using: -not working

function getpost_number(data) {      var post_number = 0;      for(var href in data.results) {          data.results[href].foreach(function(row) {              var parts = row.split("/");              console.log(parts[5]+parts[6]);          });      };      data.post_number = post_number;      return data;  } 

result:

{   "error": "bad request",   "message": "your function failed evaluate because of error: object object has no method 'split'" } 

also code inspector inside kimono had 2 warnings:

on line 7: don't make functions within loop.
on line 8: unnecessary semicolon

i appreciate , directions figure out what's wrong code above, thank you.

addendum - new attempt

here updated function i'm using code provided trincot on comments below:

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

output:

{   "error": "bad request",   "message": "your function failed evaluate because of error: parts not defined" } 

kimono inspector warnings

line 5: assignment in conditional expression.
line 8: don't make functions within loop.

the data provided not valid json, had more closing braces opening ones, , had curly double quotes. fixed in question later.

here function add post_number property in title objects, provided href property contains folder name numerical.

when run snippet, output result (json) additional properties:

function addpostnumbers(data) {      var collection, rows, i, parts;      (collection in data.results) {          var rows = data.results[collection];          (i=0; i<rows.length; i++) {              parts = rows[i].title.href.match(/\/(\d+)\//);              if (parts) {                  rows[i].title.post_number = parts[1];              }          }      }      return data;  }    // test data  var data = {    "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/"        }      ]    }  };    // don't add code. kimono (i suppose):  addpostnumbers(data);    // write result in document, don't add in own code  document.write('<pre>' + json.stringify(data, null, 2) + '</pre>');

concerning warnings received:

on line 7: don't make functions within loop.

you can ignore warning. intended avoid runtime has create function again , again, because defined within loop. here concerns foreach callback function, foreach call appears in loop itself. however, foreach construct runtimes parse efficiently.

on line 8: unnecessary semicolon

this concerns semi-colon after closing brace of for-loop. should remove one.

addendum - feed back

you tried previous version of code (present in question) , listed problems raised kimono, seems have own javascript parser , apply stricter rules browser:

line 5: assignment in conditional expression.

i have updated above code, moving assignment out of conditional expression.

line 8: don't make functions within loop.

i had written earlier warning can ignored, have replaced foreach loop standard for loop, should not warning more.

line 16: document.write can form of eval

the calls functions addpostnumbers , document.write in snippet demo solution. part of code not intended used in code.

"message": "your function failed evaluate because of error: parts not defined"

i added var statement in code above avoid this.

i added return statement, kimono might need well, don't know.


Comments

Popular posts from this blog

routing - AngularJS State management ->load multiple states in one page -

python - GRASS parser() error -

Swift game error message -