我已经这样做了几天了,遗憾的是,我就是找不到正确的搜索词,也找不到我技能不足的正确答案。
我正在将ListServ转换为话语。我正在将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“都需要插入到标题为"tech: 18F presentation”的post:[]中(no 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
}
});发布于 2021-05-30 07:00:45
这实际上只是一个“groupBy”,使用一些字符串操作来确定层次结构。
下面是一个使用Array#reduce()并检查标题startsWith() 'Re:‘的示例。如果是这样,请使用String#replace()和trim()删除开头的'Re:‘,并保留任何空格以保持一致性。
const
input = [{ "title": "Re: Re: 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": "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": 4, "body": "Please stop saying moonshot, its not dodgecoin", "date": "Fri, 28 May 2021 20:50:04 +0000", "author": "jane_doe" }, { "title": "Re: other: Title", "id": 6, "body": "throw more java, indeed. What a moon shot.", "date": "Fri, 28 May 2021 20:50:04 +0000", "author": "john_doe2" }, { "title": "other: Title", "id": 5, "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" },],
byTopic = input.reduce((acc, { title, ...post }) => {
if (title.startsWith('Re:')) {
const topic = title.replace(/^(?:Re: ){1,}/, '').trim();
acc[topic] || (acc[topic] = { posts: [] });
acc[topic].posts.push({ title: topic, ...post });
// using nullish coalescing assignment
// (acc[topic] ??= { posts: [] }).posts.push({ title: topic, ...post });
} else {
acc[title] = {
id: post.id, // your example changes id in the expected output, unsure of the logic
title: title,
pinned: false, // not sure where pinned is coming from
posts: acc[title] ? acc[title].posts : []
// using optional chaining and nullish coalescing
// posts: acc[title]?.posts ?? []
}
}
return acc;
}, {})
console.log(Object.values(byTopic));.as-console-wrapper { max-height: 100% !important; top: 0; }
正如我在评论中提到的,以这种方式使用reduce()可以直接替换为外部累加器和for循环。这里使用的是for...of
const input = [{ "title": "Re: Re: 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": "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": 4, "body": "Please stop saying moonshot, its not dodgecoin", "date": "Fri, 28 May 2021 20:50:04 +0000", "author": "jane_doe" }, { "title": "Re: other: Title", "id": 6, "body": "throw more java, indeed. What a moon shot.", "date": "Fri, 28 May 2021 20:50:04 +0000", "author": "john_doe2" }, { "title": "other: Title", "id": 5, "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" },];
const byTopic = {};
for (const { title, ...post } of input) {
if (title.startsWith('Re:')) {
const topic = title.replace(/^(?:Re: ){1,}/, '').trim();
byTopic[topic] || (byTopic[topic] = { title: topic, posts: [] });
byTopic[topic].posts.push({ title: topic, ...post });
} else {
byTopic[title] = {
id: post.id,
title: title,
pinned: false,
posts: [],
...byTopic[title]
}
}
};
console.log(Object.values(byTopic));.as-console-wrapper { max-height: 100% !important; top: 0; }
发布于 2021-05-30 07:03:21
下面是一个使用Array.forEach运算符的简单解决方案。可能有更有效的解决方案,如果您有一个大型数据集,这可能很重要:
const source = [{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"}];
let = formatted = [];
// Loop over the original data, find parents
source.forEach((item) => {
// Is not a reply
if (item.title.indexOf('Re:') === -1) {
formatted.push(item);
}
});
// Find children, append to parents
source.forEach((item) => {
formatted.forEach((parent) => {
// Child contains parent title.
if (item.title.indexOf(parent.title) !== -1 && item.title.indexOf('Re:') !== -1) {
if (!parent.data) {
parent.data = [];
}
parent.data.push(item);
}
})
});
console.log(formatted);https://stackoverflow.com/questions/67756233
复制相似问题