0

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

13
  • No, it's not, if you're not going to store the resulted array map returns. If you don't need the array, use forEach instead of map. It's also notable, that map works with arrays only, you can't iterate an object using map. Commented Jun 9, 2021 at 7:27
  • ok but fetching type from nested object can be possible by forEach function ? @Teemu Commented Jun 9, 2021 at 7:29
  • forEach has lower performance than plain for loop, so you may use for loop on this. Commented Jun 9, 2021 at 7:30
  • 2
    what exactly is the question here? Commented Mar 26, 2022 at 21:10
  • if you have resolved this question, you should close it to stop people from wasting their time trying to help you. Commented Mar 27, 2022 at 19:07

1 Answer 1

2

Hello in this case I will say that trying to user Array function will make the code too unclear. Sometime you may need to use function ;)

let result = {}

const mapMedia = (val) => {
    if (val instanceof Array) {
        return val.map(mapMedia).filter(i => !!i)
    }
    if (val && val.conf && val.conf.type === 'media' && val.conf.id) {
        return mediaObj[val.conf.id] || null
    }
    return undefined
}

const getLocalizedDataForAuthor = (locale, author) => {
    return result && result[locale] && result[locale][author] || null
}

const addLocalizedDataForAuthor = (locale, author, key, value) => {
    if (key && value) {
        if (!result[locale]) { // create the locale in result if not existing
            result[locale] = {}
        }
        if (!result[locale][author]) { // create the author in result if not existing
            result[locale][author] = {}
        }
        result[locale][author][key] = value
    }
    
}

data.forEach(authorInitialData => { // authorInitialData : {system, DataDetails}
    for (const key in authorInitialData.DataDetails) {
        const localizedData = authorInitialData.DataDetails[key]
        for (const locale in localizedData) {
            const authorFinalData = {}
            var finalData = localizedData[locale]
            const media = mapMedia(finalData)
            if (media !== undefined) { // is not a media
                addLocalizedDataForAuthor(locale, authorInitialData.system.id, key, media)
            } else {
                addLocalizedDataForAuthor(locale, authorInitialData.system.id, key, finalData)
            }
            const author = getLocalizedDataForAuthor(locale, authorInitialData.system.id)
            if (author) {
                author.subject = author.subject || 'unknown'
                author.address = author.address || '/new/unknown'
            }
        }
    }
})

console.log(result)

If you really want to use only Array function you will hae to use Object.keys(yourObject).arrayFunction( arrayFunctionCallback )

Sign up to request clarification or add additional context in comments.

2 Comments

Hi @Merlin sorry but I have solved that question :) can you help me in the updated question ?
I've updated the code, I had to make only a few changes to make it match you're demande. Also you removed the mediaObj snippet from your original question so it's almost impossible to respond you demande.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.