Monday, 28 March 2022

replace with the id if it match with id of other using node js / javascript

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" },
        gallery: {
          "za-op": [
            {
              conf: {
                type: "media",
                id: "01",
              },
            },
            {
              conf: {
                type: "media",
                id: "10",
              },
            },
          ],
        },
        mediaAccess: {
          hi: {
            conf: {
              type: "media",
              id: "01",
            },
          }, 
          en: {
            conf: {
              type: "media",
              id: "1000",
            },
          },
        },
      },
    },
    {
      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" },
        entryDetails: {
          "hi-In": [
            {
              conf: {
                type: "entry",
                id: "100",
              },
            },
            {
              conf: {
                type: "entry",
                id: "34",
              },
            },
          ],
        },
        singleEntry: {
          "en-us": {
            conf: {
              type: "entry",
              id: "34",
            },
          },
        },
      },
    },
    {
      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" },
        media: {
          hi: {
            conf: {
              type: "media",
              id: "01",
            },
          },
        },
      },
    },
    {
      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] ??= {};
          if (typeof value !== "object") {
            r[name][loc][id][key] = value;
          } else {
            console.log(value);
          }
        });
      });
      return r;
    },
    {}
  );

console.log(result)

In the above output I am trying to replace the media with the below object as the below code is for the media I want to match the id which is present inside the data output and replace the object with the mediaObj matching id

let mediaObj = {
  "01": {
    status: true,
    tag: [],
    filename: "exute-image.jpg",
    is_dir: false,
    parent_uid: null,
  },
  "02": {
    status: true,
    tag: [],
    filename: "wallpapers-6.jpg",
    is_dir: false,
    parent_uid: null,
  },
  "10": {
    status: true,
    tag: [],
    filename: "in-space-rk.jpg",
    is_dir: false,
    parent_uid: null,
  },
  "25": {
    status: true,
    tag: [],
    filename: "justice-league.jpg",
    is_dir: false,
    parent_uid: null,
  },
  "67": {
    status: true,
    tag: [],
    filename: "batman.jpg",
    is_dir: false,
    parent_uid: null,
  },
};

so my expected output is like this when I run my program

Expected Value

{
  "Author": {
    "en-us": {
      "4gSSbjCFEorYXqrgDIP2FA": {
        "shortSlugOption": "some value",
        "mediaFileAssetLink": "some file",
        "singleMediaImage": "english link"
      },
      "1aBOO8tu3lUsjtICuIbUM5": {}
    },
    "za-op": {
      "4gSSbjCFEorYXqrgDIP2FA": {
        "shortSlugOption": "random value",
        "mediaFileAssetLink": "file Linl"
      },
      "1aBOO8tu3lUsjtICuIbUM5": {
        "short": "short details",
        "gallery": [
          {
            "status": true,
            "tag": [],
            "filename": "exute-image.jpg",
            "is_dir": false,
            "parent_uid": null
          },
          {
            "status": true,
            "tag": [],
            "filename": "in-space-rk.jpg",
            "is_dir": false,
            "parent_uid": null
          }
        ]
      }
    },
    "hi": {
      "4gSSbjCFEorYXqrgDIP2FA": {
        "mediaFileAssetGalary": "file link 2",
        "singleMediaImage": "file link single",
        "mediaAccess": [
          {
            "status": true,
            "tag": [],
            "filename": "exute-image.jpg",
            "is_dir": false,
            "parent_uid": null
          }
        ]
      }
    },
    "hi-In": {
      "1aBOO8tu3lUsjtICuIbUM5": {
        "shortSlugOption": "options",
        "booleanField": "true"
      }
    }
  }
}

as the id get changed and I want to match every-time with the mediaObj object and replace the value if there is multiple or single media file exits but if the media id does not match with mediaObj then it should delete the that object as you can see in mediaAccess id 1000 does not match with the mediaObj so it should delete it

as the same applies with the entries if I get the media replace code then I can try with the entry code too



from replace with the id if it match with id of other using node js / javascript

No comments:

Post a Comment