使用 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 中沒有“標題: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' }]
唯一可能的連接屬性是兩個 Arrs 中看到的tabTitle和title屬性,但一個 title 屬性具有TextI DontWant。我希望 tabTitle: 'tabTitleTwo' 和 id: NULL 在新資料集中可用,因為這將顯示在我的 ejs 頁面上呈現時的任何間隙。
到目前為止,這是我嘗試 洗掉不需要的文本:
var wikiArr2= wikiArr.map(function(v) {
return v.toString().replace(/ TextI DontWant/g, '');
});
要合并 2 個陣列:
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': ']' } ]
……
我似乎需要幫助的專案是:
- 如何從資料集 wikiArr 的標題中過濾掉我不想要的文本,無論是在呈現資料之前還是之后?
- 我該如何做我假設是 If Else 來匹配或比較第一個和第二個資料集的標題(或其他比較?)
- 有沒有比使用 ... spread 更好的方法將 2 個資料集合并到 1 個中?
為串列道歉,很高興決議出單獨的專案。
非常感謝!
uj5u.com熱心網友回復:
假設以wikiArr.title開頭dbtabArr.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);
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/503959.html
