-1

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

7
  • 2
    It's unclear what you think the problem is: "the output" is an array of arrays. Where/how are you looking at the output, and what makes you think it's "wrong"? Commented May 26, 2021 at 20:04
  • What is the use case you are trying to solve, since arrays need to be stored inside a data structure. You can convert them to strings ( JSON.stringify(res[0])+JSON.stringify(res[1])) and apply custom regex to use it. Commented May 26, 2021 at 20:10
  • Hello @DaveNewton i update my question i hope its now understandable ? :) Commented May 26, 2021 at 20:11
  • 1
    It isn't; where/how are you looking at the output, and what makes you think it's wrong? If you want to generate a CSV then you need to iterate over your mapped array and create CSV from each entry. Commented May 26, 2021 at 20:22
  • 2
    You should reduce the example: there is a lot of data in it that is unrelated to your question. Secondly, you should show your efforts. What is the problem you encounter? Where is your code? Finally, you have completely re-edited the question which is nothing like the first version of the question. What's all that about? Oh, and that is not JSON. Please read the usage description of the json tag. Commented Mar 23, 2022 at 16:51

2 Answers 2

1
+150

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"
      }
    }
  }
}
Sign up to request clarification or add additional context in comments.

Comments

0

Bro... You're asking a very simple question in a really complex way... I am not 100% sure what you are asking and what data you want to replace with what... but I'll try my best :)

When you receive the data in JSON format, you will have to manipulate it into a JS object that you can extract data from

let parsedData = JSON.parse(JSON.stringify(data))

This will allow you to easily access keys and values within the data you received After that, you can build a constructor function that builds the new object for you, by accepting the received data object, that has been parsed, and the data you want to replace the values within the data you receive

const constructNewData = (parsedData, replaceData) => {
    let newData = {};
    Object.keys(parsedData).forEach(key => {
        if (replaceData[key]) {
            newData[key] = replaceData[key];
        } else {
            newData[key] = parsedData[key];
        }
    });
    return newData;
}

You're going to want to do something like this. Map over the keys of your received data and replace the values if they match

If you can exactly explain which values you want to replace in your JSON data, I can help you further :)

1 Comment

for clear understanding you can see my expected output like where I have share how my output look like

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.