Search code examples
javascriptnode.jsnodes

add new json object if id match with json 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 json 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 json 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 json 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


Solution

  • I think you're looking for this:

    Replace

              } else {
                console.log(value);
              }
    

    with this:

            } else {
              const newValues = Array.isArray(value) ? value.map(replacePossibleConf) : [ replacePossibleConf(value) ];
              if (newValues.filter(e => e != null).length > 0) {
                r[name][loc][id][key] = newValues;
              }
            }
    

    and add the function:

    function replacePossibleConf(possibleConf) {
      return mediaObj[possibleConf?.conf?.id];
    }
    

    Which gives output very close to what you were asking:

    {
      "Author": {
        "en-us": {
          "4gSSbjCFEorYXqrgDIP2FA": {
            "shortSlugOption": "some value",
            "mediaFileAssetLink": "some file",
            "singleMediaImage": "english link"
          },
          "1aBOO8tu3lUsjtICuIbUM5": {}
        },
        "za-op": {
          "4gSSbjCFEorYXqrgDIP2FA": {
            "shortSlugOption": "random value",
            "mediaFileAssetLink": "file Linl",
            "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
              }
            ]
          },
          "1aBOO8tu3lUsjtICuIbUM5": {
            "short": "short details"
          }
        },
        "hi": {
          "4gSSbjCFEorYXqrgDIP2FA": {
            "mediaFileAssetGalary": "file link 2",
            "singleMediaImage": "file link single",
            "mediaAccess": [
              {
                "status": true,
                "tag": [],
                "filename": "exute-image.jpg",
                "is_dir": false,
                "parent_uid": null
              }
            ]
          }
        },
        "en": {
          "4gSSbjCFEorYXqrgDIP2FA": {}
        },
        "hi-In": {
          "1aBOO8tu3lUsjtICuIbUM5": {
            "shortSlugOption": "options",
            "booleanField": "true"
          }
        }
      },
      "testing": {
        "en-us": {
          "2pOUGnI1oRD7nsrYs600HA": {
            "testingNewValue": "details value"
          },
          "7kRzyt4PFrX13gHcw3Z1Ko": {
            "testingNewValue": "kknksdo"
          }
        }
      },
      "new": {
        "hi": {
          "66rzYr2BpWL1VTBHdLTdSW": {
            "oneReference": "values 1",
            "media": [
              {
                "status": true,
                "tag": [],
                "filename": "exute-image.jpg",
                "is_dir": false,
                "parent_uid": null
              }
            ]
          }
        },
        "hi-In": {
          "66rzYr2BpWL1VTBHdLTdSW": {
            "multiReference": "values 2"
          }
        },
        "hi-IN": {
          "cIb5mqEBRWDD6hrNmFmFE": {
            "testingNewValue": "jksdsdo"
          }
        }
      },
      "dummy": {
        "za-op": {
          "2OspeCtNK0sh2cPiuU9jIz": {
            "short": "dfvndkssa"
          },
          "2eAmIIuG4xkLvatkU3RUSy": {
            "india": "sdci"
          }
        },
        "hi": {
          "2OspeCtNK0sh2cPiuU9jIz": {
            "shortSlugOption": "sdocjosmdc"
          },
          "2eAmIIuG4xkLvatkU3RUSy": {
            "india": "hewd"
          },
          "7hbdS3MgfZ73TOtlu1WfXw": {
            "locationField": "sdcndkdc"
          }
        },
        "en-us": {
          "2OspeCtNK0sh2cPiuU9jIz": {
            "mediaFileAssetLink": "jdsojocis"
          },
          "2eAmIIuG4xkLvatkU3RUSy": {
            "dummy": "dshcifdvk"
          },
          "7hbdS3MgfZ73TOtlu1WfXw": {
            "testingNewValue": "sdcoklsdc"
          }
        },
        "hi-In": {
          "2OspeCtNK0sh2cPiuU9jIz": {
            "booleanField": "true"
          }
        }
      }
    }