通过在 javascript / node js 中添加键来创建自定义嵌套对象

creating cutom nested object by adding key in javascript / node js

let 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": "details of shorts", "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" },
    },
  },
];

let res = data.reduce((acc, curr) => {
  if (!acc[curr.system.content.type.name]) {
    acc[curr.system.content.type.name] = {};
  }

  let detailsObj = {};
  Object.keys(curr.DataDetails).forEach((detail) => {
    detailsObj[detail] = Object.values(curr.DataDetails[detail])[0];
  });

  acc[curr.system.content.type.name][curr.system.id] = {
    ...detailsObj,
  };
  return acc;
}, {});

console.log(res);

我将我的数据存储在 JSON 文件中,但通过对当前对象进行一些修改。

我要存入JSON的数据是这样的

喜欢数据存在

  1. 作者
  2. en-us/zo-ap / obj
  3. 中可用的其他键
  4. 4gSSbjCFEorYXqrgDIP2FA/ 1aBOO8tu3lUsjtICuIbUM5
  5. 然后剩下的json对象

预期输出

{
    "Author": {
        "en-us": {
            "4gSSbjCFEorYXqrgDIP2FA": {
                "shortSlugOption": "some value",
                    "mediaFileAssetLink": "some file",
                  "singleMediaImage": "english link"
            },
            "1aBOO8tu3lUsjtICuIbUM5": {
                "short":"details of shorts"
            }
        },
        "za-op": {
            "4gSSbjCFEorYXqrgDIP2FA": {
                "shortSlugOption": "random value",
               "mediaFileAssetLink":"file Linl", 
            },
            "1aBOO8tu3lUsjtICuIbUM5": {
                "short":"short details",
            }
        },
        "hi": {
            "4gSSbjCFEorYXqrgDIP2FA": {
                "mediaFileAssetGalary": "file link 2",
                  "singleMediaImage":"file link single"
            }
        },
        "hi-In": {
            "1aBOO8tu3lUsjtICuIbUM5": {
                "shortSlugOption": "options",
                    "booleanField":true
            }
        }
    },
    "dummy": {
        "en-us": {
            "2OspeCtNK0sh2cPiuU9jIz": {                
                "mediaFileAssetLink":"jdsojocis",
            },
            "2eAmIIuG4xkLvatkU3RUSy": {
             "dummy": "dshcifdvk"
            }
            "7hbdS3MgfZ73TOtlu1WfXw": {
                "testingNewValue":"sdcoklsdc"
            }
        },
        "za-op": {
            "2OspeCtNK0sh2cPiuU9jIz": {                
                "short":"dfvndkssa",  
            },
            "2eAmIIuG4xkLvatkU3RUSy": {
                "india":"sdci"
            }
        },
        "hi": {
            "2OspeCtNK0sh2cPiuU9jIz": {
                "shortSlugOption":"sdocjosmdc"
            },
            "2eAmIIuG4xkLvatkU3RUSy": {
                "india":"hewd"
            }
        }
        "hi-In": {
            "2OspeCtNK0sh2cPiuU9jIz": {                
                "booleanField":"true"
            },
            "7hbdS3MgfZ73TOtlu1WfXw": {
                "locationField":"sdcndkdc"
            }
        }
    },
    "new": {
        "hi": {
            "66rzYr2BpWL1VTBHdLTdSW": {
                "oneReference":"values 1"
            }
        },
        "hi-In": {
            "66rzYr2BpWL1VTBHdLTdSW": {
                "multiReference":  "values 2" ,
            },
            "hi-IN": {
                "cIb5mqEBRWDD6hrNmFmFE": {
                    "testingNewValue":"jksdsdo"
                }
        }
        }
    }.
    "testing": {
        "en-us": {
            "2pOUGnI1oRD7nsrYs600HA": {
                "testingNewValue":"details value"
            },
            "7kRzyt4PFrX13gHcw3Z1Ko": {
                "testingNewValue":"kknksdo" 
            }
        },
    }
}

正如上面的代码中,en-us hi hi-In hi-IN zo-ap 或任何其他键正在被删除

但我想用这个名称或 json

中存在的任何其他名称创建一个嵌套的 json 对象

您需要嵌套条目并按更改后的顺序使用键。

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": "details of shorts", "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(([k, o]) => {
            Object.entries(o).forEach(([l, v]) => {
                r[name][l] ??= {};
                r[name][l][id] ??= {};
                r[name][l][id][k] = v;
            });
        });
        return r;
    }, {});

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }