Encountering an issue with merging two objects while working with the Contentful API. The data structure for links provided by the API is separated into arrays of objects, making further processing complex due to the absence of a single object. Each object in the array is linked by an id. The objective is to merge or potentially replace these objects.
Below is the data structure:
const dataDictionary = {
includes: {
entry: [
{
fields: {data: 'https://link1.com'},
sys: {id: 12345}
},
...
]
},
items: [
{
fields: {
urls: [
{
id: 14345,
type: 'link'
},
...
],
...
}
},
..
]
}
The goal is to combine and modify all arrays containing links within the items, using the actual link values from includes.
Here's the code being used:
const mergeByValue = (arrayTo, arrayFrom) => {
const finalMerge = arrayTo.map(itm => ({
...arrayFrom.find((item) => (item.sys.id === itm.id) && item),
...itm
}));
return finalMerge;
}
const parseDataDictionary = (dataDictionary) => {
const pages = dataDictionary.items;
const includes = dataDictionary.includes.Entry;
pages.map(page => {
return Object.entries(page.fields).map(([key, value]) => {
if (Object.prototype.toString.call(value) === '[object Array]') {
return mergeByValue(value, includes);
}
})
})
}
parseDataDictionary(dataDictionary);
The merging process appears to be functioning correctly, yet the expected merged values are not being returned. Any insight on resolving this issue would be greatly appreciated. Thank you!
UPD: The desired outcome:
{
items: [
{
fields: {
urls: [
{
id: 14345,
type: 'link',
data: 'https://link3.com'
},
...
],
...
}
},
...
]
}