如果 id 与其他使用节点 js 的 json id 匹配,则添加新的 json 对象 / javascript

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)

在上面的输出中,我试图用下面的对象替换媒体,因为下面的代码是针对媒体的 我想匹配数据输出中存在的 id,并将 json 替换为匹配 id

的 mediaObj
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,
  },
};

所以当我 运行 我的程序

时,我的预期输出是这样的

预期价值

{
  "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"
      }
    }
  }
}

随着 id 的改变,我想每次都与 mediaObj json 匹配,如果有多个或单个媒体文件存在但媒体 id 不存在,则替换值匹配 mediaObj 那么它应该删除那个 json 正如你在 mediaAccess 中看到的那样 id 1000 不匹配 mediaObj 所以它应该删除它

同样适用于条目,如果我得到媒体替换代码,那么我也可以尝试使用条目代码

兄弟……你问一个非常简单的问题却用了非常复杂的方式…… 我不是 100% 确定你在问什么以及你想用什么替换什么数据......但我会尽力:)

当您收到 JSON 格式的数据时,您必须将其处理成一个 JS 对象,您可以从中提取数据

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

这将使您能够轻松访问您收到的数据中的键和值 之后,您可以构建一个构造函数来为您构建新对象,方法是接受已解析的接收到的数据对象,以及您要替换接收到的数据中的值的数据

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;
}

你会想要做这样的事情。映射您收到的数据的键并替换匹配的值

如果您能准确说明要在 JSON 数据中替换哪些值,我可以进一步帮助您:)

我想你正在寻找这个:

替换

          } else {
            console.log(value);
          }

有了这个:

        } 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;
          }
        }

并添加函数:

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

输出结果非常接近您的要求:

{
  "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"
      }
    }
  }
}