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 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 to evaluate because of Error: Object object has no method 'split'"
}
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.
Addendum - new attempt
Here is the updated function
I'm using with the code provided by Trincot on the 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 to evaluate because of Error: parts is not defined"
}
Kimono Inspector warnings
line 5: Assignment in conditional expression.
line 8: Don't make functions within a loop.