let data = [{
system: {
id: "4gSSbjCFEorYXqrgDIP2FA",
type: "Entry",
name: "User"
},
DataDetails: {
shortOption: {
"en-us": "some value"
},
mediaFile: [{
sys: {
type: "Link",
link: "Entry",
id: "7kRzyt4PFo",
},
},
{
sys: {
type: "Link",
link: "Entry",
id: "2OspeCtNK0s",
},
},
],
mediaGalary: [{
sys: {
type: "Link",
link: "Asset",
id: "gHcw3Z1Ko",
},
},
{
sys: {
type: "Link",
linkType: "Asset",
id: "h2cPiuU9jIz",
},
},
],
mediaExplore: [{
sys: {
type: "Link",
link: "Asset",
id: "euwcbds3282",
},
},
{
sys: {
type: "Link",
linkType: "Asset",
id: "jndsiw23912",
},
},
],
singleMediaImage: {
sys: {
type: "Link",
linkType: "Asset",
id: "7kRzyt4PFo",
},
},
},
},
{
system: {
id: "1aBOO8tu3lUsjtICuIbUM5",
type: "Entry",
name: "User"
},
DataDetails: {
short: {
"en-us": "details of shorts"
},
shortSlugOption: {
"hi-In": "options"
},
booleanField: {
kl: "true"
},
},
},
{
system: {
id: "2pOUGnI1oRD7nsrYs600HA",
type: "Entry",
name: "Dummy"
},
DataDetails: {
testingNewValue: [{
sys: {
type: "Link",
link: "Entry",
id: "66rzYr2BpWL",
},
},
{
sys: {
type: "Link",
link: "Entry",
id: "1VTBHdLTdSW",
},
},
],
},
},
{
system: {
id: "66rzYr2BpWL1VTBHdLTdSW",
type: "Entry",
name: "new"
},
DataDetails: {
oneReference: {
sys: {
type: "Link",
linkType: "Asset",
id: "h2cPiuU9jIz",
},
},
multiReference: [{
sys: {
type: "Link",
link: "Asset",
id: "gHcw3Z1Ko",
},
},
{
sys: {
type: "Link",
link: "Asset",
id: "h2cPiuU9jIz",
},
},
],
},
},
{
system: {
id: "cIb5mqEBRWDD6hrNmFmFE",
type: "Entry",
name: "new"
},
DataDetails: {
testingNewValue: {
"hi-IN": "jksdsdo"
}
},
},
{
system: {
id: "7kRzyt4PFrX13gHcw3Z1Ko",
type: "Entry",
name: "Dummy"
},
DataDetails: {
testingNewValue: {
"en-us": "kknksdo"
}
},
},
];
let anotherObj = {
"gHcw3Z1Ko": {
status: true,
tag: [],
filename: "exute-image.jpg",
is_dir: false,
parent_uid: null,
},
"h2cPiuU9jIz": {
status: true,
tag: [],
filename: "wallpapers-6.jpg",
is_dir: false,
parent_uid: null,
},
"7kRzyt4PFo": {
status: true,
tag: [],
filename: "in-space-rk.jpg",
is_dir: false,
parent_uid: null,
},
"euwcbds3282": {
status: true,
tag: [],
filename: "justice-league.jpg",
is_dir: false,
parent_uid: null,
},
"jndsiw23912": {
status: true,
tag: [],
filename: "batman.jpg",
is_dir: false,
parent_uid: null,
},
};
let res = data.reduce((acc, curr) => {
if (!acc[curr.system.name]) {
acc[curr.system.name] = {};
}
let detailsObj = {};
let assetArray=[];
for (let key of Object.keys(curr.DataDetails)) {
detailsObj[key] = Object.values(curr.DataDetails[key])[0];
if (typeof Object.values(curr.DataDetails[key])[0] === "object") {
Object.values(curr.DataDetails[key]).map((type) => {
if (Array.isArray(type)) {
type.map((link) => {
if (link.sys.link === "Asset") {
for (let id in anotherObj) {
if (link.sys.id === id) {
assetArray.push(anotherObj[id]);
detailsObj[key] = assetArray;
}
}
}
});
} else {
if (type.sys !== undefined) {
// for single Asset
if (type.sys.link === "Asset") {
for (let id in anotherObj) {
if (type.sys.id === id) {
detailsObj[key] = [anotherObj[id]];
}
}
}
}
}
});
}
}
acc[curr.system.name][curr.system.id] = {
title: curr.system.id,
uid: curr.system.id,
url: `/${curr.system.name}/${curr.system.id}`,
...detailsObj,
};
return acc;
}, {});
console.log(res);
I am trying to put two object data in one output but by reading the link which is Asset
but in the multiple Array list the output is displaying as single Asset output as mediaFile contain two entries but I don't know where I am doing wrong and it's displaying me a single value
as my main purpose is to replace the sys:{id:erfrdvsdf} by the anotherObj key data
so it should look like this for example my output is like this
"User": {
"4gSSbjCFEorYXqrgDIP2FA": {
"title": "4gSSbjCFEorYXqrgDIP2FA",
"uid": "4gSSbjCFEorYXqrgDIP2FA",
"url": "/User/4gSSbjCFEorYXqrgDIP2FA",
"shortOption": "some value",
"mediaFile": {
"sys": {
"type": "Link",
"link": "Entry",
"id": "7kRzyt4PFo"
}
},
"mediaGalary": [
{
/**id:7**/
"status": true,
"tag": [],
"filename": "in-space-rk.jpg",
"is_dir": false,
"parent_uid": null
}
],
"singleMediaImage": {
"type": "Link",
"linkType": "Asset",
"id": "h2cPiuU9jIz"
}
},
},
but it should like this
"User": {
"4gSSbjCFEorYXqrgDIP2FA": {
"title": "4gSSbjCFEorYXqrgDIP2FA",
"uid": "4gSSbjCFEorYXqrgDIP2FA",
"url": "/User/4gSSbjCFEorYXqrgDIP2FA",
"shortOption": "some value",
"mediaFile": {
"sys": {
"type": "Link",
"link": "Entry",
"id": "7kRzyt4PFo"
}
},
"mediaGalary": [
{
"status": true,
"tag": [],
"filename": "in-space-rk.jpg",
"is_dir": false,
"parent_uid": null
},
{
"status": true,
"tag": [],
"filename": "wallpapers-6.jpg",
"is_dir": false,
"parent_uid": null
},
],
"mediaExplore": [
{
"status": true,
"tag": [],
"filename": "justice-league",
"is_dir": false,
"parent_uid": null
},
{
"status": true,
"tag": [],
"filename": "batman.jpg",
"is_dir": false,
"parent_uid": null
},
],
"singleMediaImage": [
{
"status": true,
"tag": [],
"filename": "wallpapers-6.jpg",
"is_dir": false,
"parent_uid": null
},
]
},
},
but my expected output is what I am looking to it
Due to this I am stuck with the entry field as the same problem I am going through with entry field as I wrote same code to fetch the entry fields which are in Array and for the single entry field
where my code break I don't know
I know this is long code but help me out to solve this error as due to this I am not moving ahead