修改数据,爆炸字符串和在和服上添加新属性的功能 [英] Function to modify data, explode string and add new a new property on kimono
问题描述
我正在使用Kimono提取一些数据并创建一个API:
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/"
}
]
}
}
我正试图在title元素中获取href的值,然后爆炸字符串以获取ID号(上面代码中的9418,9422,9358,9419)并创建一个只有id号的新属性。
或者,如果无法创建新属性,那么我想只是r eplace所有的href字符串并保留id号而不是完整的href url。
这是我的不工作功能:
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 my not working function:
// 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>');
结果:
{
错误:错误请求,
消息:由于错误,您的功能无法评估:意外的标识符
}
我尝试过:
//主要功能
函数addPostNumbers(数据){
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];
}
}); < br $>
}
}
//调用主函数
addPostNumbers(数据);
//在文档中写入结果。
document.write('< pre>'+ JSON.stringify(data, null,2)+'< / pre>');
Result:
{
"error": "Bad Request",
"message": "Your function failed to evaluate because of Error: Unexpected identifier"
}
What I have tried:
// 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>');
推荐答案
我会搜索集合
因为它不是一个索引和
data.results [collection]
对我非常怀疑。
I would search aroundcollection
because it is not an index and
data.results[collection]
is highly suspect to me.
这篇关于修改数据,爆炸字符串和在和服上添加新属性的功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!