如何在过滤掉某些文本后合并数据集



使用Nodejs、Express、Neo4j数据和API数据

很抱歉,如果其他地方有答案,请给我指个方向。

在我的server.js文件中,我有一组Neo4j数据:dbtabArr

[ { dbTitle: 'dbTitleOne',
tabTitle: 'tabTitleOne' },
{ dbTitle: 'dbTitleOne',
tabTitle: 'tabTitleTwo' },
{ dbTitle: 'dbTitleOne',
tabTitle: 'tabTitleThree' }]

另一组API数据:wikiArr

[ { id: 'abc123',
title: 'tabTitleOne TextIDontWant'},
{ id: 'def456',
title: 'tabTitleThree TextIDontWant'}]

请注意,此wikiArr中没有"title:tabTitleTwo",有时dbtabArr中也不会有1:1的对应项

我想加入数据集,我知道我可以做一些类似的事情

const obj1 = dbtabArr;
const obj2 = wikiArr;

const mergedObject = {
...obj1,
...obj2
};

但这只会使顶部数据高于底部数据。

最终,我希望新数据集看起来像这样:newDataArr

[ { id: 'abc123',
dbTitle: 'dbTitleOne',
tabTitle: 'tabTitleOne' },
{ id: NULL,
dbTitle: 'dbTitleOne',
tabTitle: 'tabTitleTwo' },
{ id: 'def456',
dbTitle: 'dbTitleOne',
tabTitle: 'tabTitleThree' }]

唯一可能的联接属性是选项卡标题&title属性出现在两个Arrs中,但一个title属性具有TextI DontWant。我希望在新的数据集中有id为NULL的tabTitle:'tabTitleTwo',因为这将显示在我的ejs页面上呈现时任何间隙的位置。

以下是我迄今为止所做的尝试删除不需要的文本:

var wikiArr2= wikiArr.map(function(v) {
return v.toString().replace(/ TextI DontWant/g, '');
});

合并两个阵列:

const map = new Map();
tabTitleArr.forEach(item => map.set(item.tabTitle, item));
wikiArr2.forEach(item => map.set(item.title, {...map.get(item.title), ...item}));
const mergedArr = Array.from(map.values());

结果并不是我所希望的(使用实际返回的数据,数组的顶部对象来自dbtabArr,底部对象(拼写对象(来自wikiArr(:

[ { dbSpecId: Integer { low: 1234567, high: 0 },
dbTitle: 'dbTitle',
tabTitle: 'tabTitle' },
{ '0': '[',
'1': 'o',
'2': 'b',
'3': 'j',
'4': 'e',
'5': 'c',
'6': 't',
'7': ' ',
'8': 'O',
'9': 'b',
'10': 'j',
'11': 'e',
'12': 'c',
'13': 't',
'14': ']' } ]

我似乎需要帮助的项目有:

  1. 如何从数据集wikiArr的标题中筛选出我不想要的文本,无论是在数据渲染之前还是之后
  2. 我如何做我假设的If Else来匹配或比较第一和第二数据集的标题(或其他比较?(
  3. 有没有比使用…更好的方法将2个数据集合并到1中。。。扩散

对列表表示歉意,很乐意解析出单独的项目。

非常感谢!

假设wikiArr.titledbtabArr.tabTitle开始,我以这种方式生成了newDataArr

const dbtabArr = [
{ dbTitle: 'dbTitleOne', tabTitle: 'tabTitleOne' },
{ dbTitle: 'dbTitleOne', tabTitle: 'tabTitleTwo' },
{ dbTitle: 'dbTitleOne', tabTitle: 'tabTitleThree' },
];
const wikiArr = [
{ id: 'abc123', title: 'tabTitleOne TextIDontWant' },
{ id: 'def456', title: 'tabTitleThree TextIDontWant' },
];
const newDataArr = dbtabArr
.reduce((acc, x) => {
const [wiki] = wikiArr.filter(w => w.title.startsWith(x.tabTitle));
// const id = wiki?.id ?? null; // Nodejs 16+
const id = wiki && wiki.id ? wiki.id : null;
return [...acc, { id, ...x }];
}, []);
console.log(newDataArr);

最新更新