【发布时间】:2021-05-29 22:36:26
【问题描述】:
我已经为此工作了几天,遗憾的是我无法找到正确的搜索词或我缺乏技能的正确答案。
我正在将 ListServ 转换为 Discourse。我正在将 RSS 提要转换为 JSON。 源数据示例:
{
"title": "tech: 18F presentation",
"id": 2,
"body": "Sadly, my biggest concern is whether it will run on Linux or Windows. And I guess if they’ll thrown even more java at it.",
"date": "Fri, 28 May 2021 20:50:04 +0000",
"author": "john_doe"
},
{
"title": "Re: tech: 18F presentation",
"id": 3,
"body": "throw more java, indeed. What a moon shot.",
"date": "Fri, 28 May 2021 20:50:04 +0000",
"author": "john_doe2"
},
{
"title": "Re: tech: 18F presentation",
"id": 4,
"body": "Please stop saying moonshot, its not dodgecoin",
"date": "Fri, 28 May 2021 20:50:04 +0000",
"author": "jane_doe"
},
我的数据结构需要如下所示:
{
"topics": [
{
"id": 1,
"title": "tech: 18F presentation",
"pinned": false,
"posts": [
{
"title": "Re: tech: 18F presentation",
"id": 3,
"body": "throw more java, indeed. What a moon shot.",
"date": "Fri, 28 May 2021 20:50:04 +0000",
"author": "john_doe2"
},
{
"title": "Re: tech: 18F presentation",
"id": 4,
"body": "Please stop saying moonshot, its not dodgecoin",
"date": "Fri, 28 May 2021 20:50:04 +0000",
"author": "john_doe2"
},
]
}
]
}
我需要将带有“Re:”的每个标题插入到原始标题中。示例)任何回复,“Re tech: 18F Presentation”需要插入帖子:[] of the title: “tech: 18F presentation”(无 Re: )。
我尝试将回复分散到它自己的 json 中并将其推送到 post 数组中,但我无法弄清楚如何匹配适当的标题。
let data = [];
const original_post = [];
const reply_to_post = [];
const discourse_JSON = [];
$("item").map(function (i, article) {
const title = $(article).find("title")[0].children[0].data;
const description = $(article).find("description")[0].children[0].data;
const user_email = $(article).find("author")[0].children[0].data.match("<([^>]+)>")[1];
const link = $(article).find("link")[0].children[0].data;
const guid = $(article).find("guid")[0].children[0].data;
const date = $(article).find("pubDate")[0].children[0].data;
const name = user_email.substring(0,user_email.indexOf('@')).split("_")[0] + ' ' + user_email.substring(0,user_email.indexOf('@')).split("_")[1];
const username = user_email.substring(0,user_email.indexOf('@'))
if (
!title.toLowerCase().includes("vacancy") &&
!title.toLowerCase().includes("opportunity") &&
!title.toLowerCase().includes("retirement") &&
!title.toLowerCase().includes("position") &&
!title.toLowerCase().includes("job posting") &&
!description.toLowerCase().includes("vacancy announcement") &&
!description.toLowerCase().includes("vacancy posting") &&
!description.toLowerCase().includes("vacancies")
) {
data.push({
"title": title,
"id": i,
"body": description,
"date": date,
"author": username
}
});
【问题讨论】:
-
您将需要遍历源数据,然后使用字符串匹配构建帖子数组。到目前为止你写过什么代码吗?
标签: javascript arrays object discourse