和服:修改数据、分解字符串和添加新属性的功能
Kimono: Function to modify data, explode string and add new a new property
我正在使用Kimono提取一些数据并创建一个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
element
内GET
href
的值,然后 explode
string
获取 ID 号(9418
、9422
、9358
、9419
上面的代码(并创建一个仅包含 ID 号的新属性。
或者,如果无法创建新属性,那么我想只替换所有href
字符串并保留 id 号而不是完整的 href
url。
这是我使用的代码: -不工作
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;
}
结果:
{
"error": "Bad Request",
"message": "Your function failed to evaluate because of Error: Object object has no method 'split'"
}
和服内部的代码检查器也有 2 个警告:
第 7 行:不要在循环中创建函数。
第 8 行:不必要的分号
我感谢任何帮助和指示,以找出上述代码的问题,谢谢。
增编 - 新的尝试
这是我在以下评论中与Trincot提供的代码一起使用的更新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];
}
});
}
}
输出:
{
"error": "Bad Request",
"message": "Your function failed to evaluate because of Error: parts is not defined"
}
和服检查员警告
第 5 行:条件表达式中的赋值。
第 8 行:不要在循环中创建函数。
您最初提供的数据不是有效的 JSON,因为它的右大括号多于左大括号,并且有一些大括号的双引号。这在您的问题中已稍后修复。
下面是一个函数,它将在标题对象中添加 post_number 属性,前提是 href 属性包含数字文件夹名称。
运行此代码段时,它将输出具有其他属性的结果 (JSON(:
function addPostNumbers(data) {
var collection, rows, i, parts;
for (collection in data.results) {
var rows = data.results[collection];
for (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 this to your code. Kimono will do this (I suppose):
addPostNumbers(data);
// write result in document, don't add this in your own code
document.write('<pre>' + JSON.stringify(data, null, 2) + '</pre>');
关于您收到的警告:
第 7 行:不要在循环中创建函数。
您可以忽略此警告。这是为了避免运行时必须一次又一次地创建函数,因为它是在循环中定义的。这里它涉及forEach
回调函数,其中forEach
调用出现在循环本身中。但是,使用 forEach
构造,大多数运行时将有效地解析这一点。
第 8 行:不必要的分号
这涉及 for 循环的右大括号后的分号。你应该删除那个。
附录 - 反馈
您尝试了我的代码的先前版本(现在出现在您的问题中(并列出了Kimono提出的一些问题,Kimono似乎有自己的Javascript解析器并应用比浏览器更严格的规则:
第 5 行:条件表达式中的赋值。
我已经更新了上面的代码,将赋值移出条件表达式。
第 8 行:不要在循环中创建函数。
我之前写过可以忽略此警告,但现在我已经将foreach
循环替换为标准for
循环,因此您不应该再收到此警告。
第 16 行:document.write 可以是评估的一种形式
对函数addPostNumbers和document.write的调用仅在我的代码片段中演示解决方案。代码的该部分不应在代码中使用。
"消息": "由于错误:未定义部件,您的函数无法计算">
我在上面的代码中添加了一个 var
语句以避免这种情况。
我还添加了一个return
声明,因为和服可能也需要它,我不知道。
您的行将如下所示:
{
"title": {
"href": "http://www.tvtrailers.com/scenes/view/id/9358/title/“,
"text": "Title 1"
},
"pubDate": "February 5, 2016",
},
"index": 1,
"url": "http://www.tvtrailers.com/videos/thismonth/bydate/"
}
这意味着您想更深入地挖掘并拆分row.title.href
另外,我不确定您希望检索哪些碎片,但parts[5]
等于"id"
,parts[6]
等于9358
。这是因为http:
之后的//
将在 "http:"
和 "www.tvtrailers.com"
之间创建一个空项目。
换句话说,拆分数组将如下所示: ["http:", "", "www.tvtrailers.com", "scenes", "view", "id", "9358", "title", ""]
- 一个javascript类,它接受JSON对象并将其转换为属性属性
- 添加新元素属性
- 如何通过MongoDB驱动程序(node.js)将属性(属性名称是可变的)添加到对象中
- Javascript:属性描述符和内置属性属性之间的关系是什么
- 为什么画布中以前的圆圈的属性被较新的属性替换
- 如何在窗口打开后使用 javascript 修改浏览器属性/属性
- JS:理解可写为 Object 属性属性
- 如何获取所有以某物开头的HTML属性(属性名称,*不是*它们的值!
- 在 JSON.parse() 之后保留属性属性(可写、可配置)
- 检查对 css 属性属性的支持
- 在AngularJS中创建新资源后,无法获取新对象属性
- 如何在javascript中将属性从父属性属性赋予子属性?(OOP)
- AngularJS没有给对象添加新的属性
- 如何在上传文件后为文件对象添加新的属性
- 转换JSON数组的对象,从两个属性值创建新的属性
- 我如何在JavaScript中添加新的属性到JSON字符串
- 如何在javascript的对象中添加一个新的属性
- js树形图:将新旧属性绑定到SVG元素
- 复制Javascript对象到另一个对象,不创建新的属性
- 将对象属性复制到新对象属性 - 而不是通过引用