const data = [
{
system: {
id: "4gSSbjCFEorYXqrgDIP2FA",
type: "Entry",
content: { type: { name: "Author" } },
},
DataDetails: {
shortSlugOption: { "en-us": "some value", "za-op": "random value" },
mediaFileAssetLink: { "en-us": "some file", "za-op": "file Linl" },
mediaFileAssetGalary: { hi: "file link 2" },
singleMediaImage: { hi: "file link single", "en-us": "english link" },
},
},
{
system: {
id: "1aBOO8tu3lUsjtICuIbUM5",
type: "Entry",
content: { type: { name: "Author" } },
},
DataDetails: {
short: { "en-us": ["shorts", "values"], "za-op": "short details" },
shortSlugOption: { "hi-In": "options" },
booleanField: { "hi-In": "true" },
},
},
{
system: {
id: "2pOUGnI1oRD7nsrYs600HA",
type: "Entry",
content: { type: { name: "testing" } },
},
DataDetails: { testingNewValue: { "en-us": "details value" } },
},
{
system: {
id: "66rzYr2BpWL1VTBHdLTdSW",
type: "Entry",
content: { type: { name: "new" } },
},
DataDetails: {
oneReference: { hi: "values 1" },
multiReference: { "hi-In": "values 2" },
},
},
{
system: {
id: "cIb5mqEBRWDD6hrNmFmFE",
type: "Entry",
content: { type: { name: "new" } },
},
DataDetails: { testingNewValue: { "hi-IN": "jksdsdo" } },
},
{
system: {
id: "7kRzyt4PFrX13gHcw3Z1Ko",
type: "Entry",
content: { type: { name: "testing" } },
},
DataDetails: { testingNewValue: { "en-us": "kknksdo" } },
},
{
system: {
id: "2OspeCtNK0sh2cPiuU9jIz",
type: "Entry",
content: { type: { name: "dummy" } },
},
DataDetails: {
short: { "za-op": "dfvndkssa" },
shortSlugOption: { hi: "sdocjosmdc" },
mediaFileAssetLink: { "en-us": "jdsojocis" },
booleanField: { "hi-In": "true" },
},
},
{
system: {
id: "2eAmIIuG4xkLvatkU3RUSy",
type: "Entry",
content: { type: { name: "dummy" } },
},
DataDetails: {
dummy: { "en-us": "dshcifdvk" },
india: { "za-op": "sdci", hi: "hewd" },
},
},
{
system: {
id: "7hbdS3MgfZ73TOtlu1WfXw",
type: "Entry",
content: { type: { name: "dummy" } },
},
DataDetails: {
testingNewValue: { "en-us": "sdcoklsdc" },
locationField: { hi: "sdcndkdc" },
},
},
];
result = data.reduce(
(
r,
{
system: {
id,
content: {
type: { name },
},
},
DataDetails,
}
) => {
r[name] ??= {};
Object.entries(DataDetails).forEach(([key, object]) => {
Object.entries(object).forEach(([loc, value]) => {
r[name][loc] ??= {};
r[name][loc][id] ??= {};
r[name][loc][id][key] = value;
});
});
return r;
},
{}
);
console.log(result)
my this is my object
fields
{
"Author": { "fields": "shortSlugOption" },
"testing": { "fields": "mediaaccess" },
"dummy": { "fields": "fieldtest" },
"new": { "fields": "fieldtest" },
}
I am trying to check and assign the value which is present inside the fields object with the key which is present in side result obj and assign the value
so in the fields object
"new": { "fields": "oneReference" }
I want to check the oneReference present in new
"new": {
"hi": {
"66rzYr2BpWL1VTBHdLTdSW": {
"oneReference": "values 1",
"media": {
"conf": {
"type": "media",
"id": "01"
}
}
}
},
"hi-In": {
"66rzYr2BpWL1VTBHdLTdSW": {
"multiReference": "values 2"
}
},
"hi-IN": {
"cIb5mqEBRWDD6hrNmFmFE": {
"testingNewValue": "jksdsdo"
}
}
},
so here my expected output is like this
Expected Output
"new": {
"hi": {
"66rzYr2BpWL1VTBHdLTdSW": {
"subject": "values 1"
"id": "66rzYr2BpWL1VTBHdLTdSW",
"address": "/new/values 1"
"oneReference": "values 1",
"media": {
"conf": {
"type": "media",
"id": "01"
}
}
}
},
"hi-In": {
"66rzYr2BpWL1VTBHdLTdSW": {
"subject": "unknown"
"id": "66rzYr2BpWL1VTBHdLTdSW",
"address": "/new/unknown"
"multiReference": "values 2"
}
},
"hi-IN": {
"cIb5mqEBRWDD6hrNmFmFE": {
"subject": "unknown"
"id": "cIb5mqEBRWDD6hrNmFmFE",
"address": "/new/unknown"
"testingNewValue": "jksdsdo"
}
}
},
"Author": {
"en-us": {
"4gSSbjCFEorYXqrgDIP2FA": {
"subject": "some value",
"id": "4gSSbjCFEorYXqrgDIP2FA",
"address": "Author/some value",
"shortSlugOption": "some value",
"mediaFileAssetLink": "some file",
"singleMediaImage": "english link"
}
},
"za-op": {
"4gSSbjCFEorYXqrgDIP2FA": {
"subject": "random value",
"id": "4gSSbjCFEorYXqrgDIP2FA",
"address": "Author/random value",
"shortSlugOption": "random value",
"mediaFileAssetLink": "file Linl"
}
},
"hi": {
"4gSSbjCFEorYXqrgDIP2FA": {
"subject": "unknown",
"id": "4gSSbjCFEorYXqrgDIP2FA",
"address": "Author/unknown",
"mediaFileAssetGalary": "file link 2",
"singleMediaImage": "file link single"
}
}
}
here we can see new:hi and new:hi-In contain same id 66rzYr2BpWL1VTBHdLTdSW so its should check field object oneReference present in which id and assign that value in subject but in the new:hi-IN the id cIb5mqEBRWDD6hrNmFmFE does not contain oneReference then it should print unknown like this I want to check for Author testing dummy and many more if they are present in the fields object and check the value present or not in the result and display Unknown if the value is not present but display the actual key value
so I tried this but no help
let field = {
Author: { fields: "shortSlugOption" },
testing: { fields: "mediaaccess" },
dummy: { fields: "fieldtest" },
new: { fields: "fieldtest" },
};
const data = [
{
system: {
id: "4gSSbjCFEorYXqrgDIP2FA",
type: "Entry",
content: { type: { name: "Author" } },
},
DataDetails: {
shortSlugOption: { "en-us": "some value", "za-op": "random value" },
mediaFileAssetLink: { "en-us": "some file", "za-op": "file Linl" },
mediaFileAssetGalary: { hi: "file link 2" },
singleMediaImage: { hi: "file link single", "en-us": "english link" },
},
},
{
system: {
id: "1aBOO8tu3lUsjtICuIbUM5",
type: "Entry",
content: { type: { name: "Author" } },
},
DataDetails: {
short: { "en-us": ["shorts", "values"], "za-op": "short details" },
shortSlugOption: { "hi-In": "options" },
booleanField: { "hi-In": "true" },
},
},
{
system: {
id: "2pOUGnI1oRD7nsrYs600HA",
type: "Entry",
content: { type: { name: "testing" } },
},
DataDetails: { testingNewValue: { "en-us": "details value" } },
},
{
system: {
id: "66rzYr2BpWL1VTBHdLTdSW",
type: "Entry",
content: { type: { name: "new" } },
},
DataDetails: {
oneReference: { hi: "values 1" },
multiReference: { "hi-In": "values 2" },
},
},
{
system: {
id: "cIb5mqEBRWDD6hrNmFmFE",
type: "Entry",
content: { type: { name: "new" } },
},
DataDetails: { testingNewValue: { "hi-IN": "jksdsdo" } },
},
{
system: {
id: "7kRzyt4PFrX13gHcw3Z1Ko",
type: "Entry",
content: { type: { name: "testing" } },
},
DataDetails: { testingNewValue: { "en-us": "kknksdo" } },
},
{
system: {
id: "2OspeCtNK0sh2cPiuU9jIz",
type: "Entry",
content: { type: { name: "dummy" } },
},
DataDetails: {
short: { "za-op": "dfvndkssa" },
shortSlugOption: { hi: "sdocjosmdc" },
mediaFileAssetLink: { "en-us": "jdsojocis" },
booleanField: { "hi-In": "true" },
},
},
{
system: {
id: "2eAmIIuG4xkLvatkU3RUSy",
type: "Entry",
content: { type: { name: "dummy" } },
},
DataDetails: {
dummy: { "en-us": "dshcifdvk" },
india: { "za-op": "sdci", hi: "hewd" },
},
},
{
system: {
id: "7hbdS3MgfZ73TOtlu1WfXw",
type: "Entry",
content: { type: { name: "dummy" } },
},
DataDetails: {
testingNewValue: { "en-us": "sdcoklsdc" },
locationField: { hi: "sdcndkdc" },
},
},
];
Object.keys(field).map((type) => {
const searchKey = field[type]?.fields;
result = data.reduce(
(
r,
{
system: {
id,
content: {
type: { name },
},
},
DataDetails,
}
) => {
r[name] ??= {};
Object.entries(DataDetails).forEach(([key, object]) => {
Object.entries(object).forEach(([loc, value]) => {
r[name][loc] ??= {};
r[name][loc][id] ??= {subject:"hello", id:id, address:`${name}/hello`};
r[name][loc][id][key] = value;
});
});
console.log(r);
return r;
},
{}
);
const typeObject = result[type] ?? {};
let subject;
outer: for (key in typeObject) {
const value = typeObject[key];
subject = null;
for (subkey in value) {
const subvalue = value[subkey];
subject = subvalue[searchKey];
if (subject != null) break;
}
console.log(subject ?? "Unknown");
}
});
In place of hello I am trying to get the value of shortSlugOption and other values which are present inside the field Object but if the shortSlugOption present in en-us za-op or hi then it should take first value from this key if the shortSlugOption is not present inside en-us then it should print unknown in en-uswhile in za-op and assign that value but if in en-us and za-op the value is not present then it should print unknown and check the other hi like this but as we can see Author en-us 1aBOO8tu3lUsjtICuIbUM5 does not contain shortSlugOption then it should print unknown in place of hello
mapreturns. If you don't need the array, useforEachinstead ofmap. It's also notable, thatmapworks with arrays only, you can't iterate an object usingmap.forEachhas lower performance than plainforloop, so you may useforloop on this.