How to Merge Data Sets After Filtering Out Certain Text?

Issue

Using Nodejs, Express, Neo4j data and API data

Apologies if this is answered elsewhere, please point me in that direction.

On my server.js file, I have a set of Neo4j data: dbtabArr

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

And another set of API data: wikiArr

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

NOTE that there is NO ‘title: tabTitleTwo’ in this wikiArr, sometimes there will not be a 1:1 counterpart in the dbtabArr.

I want to join the datasets and I understand I could do something like

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

But that would just put the top data over the bottom data.

Ultimately, I would like the new data set to look like this: newDataArr

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

The only possible join attribute is the tabTitle & title properties seen in both Arrs, but the one title property has TextI DontWant.
And I am hoping to have the tabTitle: ‘tabTitleTwo’ with the id: NULL available in the new data set as that will show where any gaps are when rendered on my ejs page.

Here’s what I’ve tried so far
To remove the unwanted text:

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

To merge the 2 arrays:

      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());

The result is not what I was hoping for (using real returned data, the top object of the array is from dbtabArr and the bottom object (spelling out object) is from 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': ']' } ]

……

The items I seem to need help with are:

  1. How can I filter out the text I don’t want from the title in data set wikiArr, whether before or after data is rendered?
  2. How do I do what I am assuming is an If Else to match or compare the titles from the first and second data sets (or other comparison?)
  3. Is there a better way to merge the 2 data sets into the 1 than using … spread?

Apologies for the list, happy to parse out to separate items.

Many Thanks!

Solution

Having assumed that wikiArr.title starts with dbtabArr.tabTitle, I’ve generated the newDataArr this way:

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);

Answered By – Mr. R

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published