基于。(点)的嵌套对象

时间:2019-11-22 11:15:14

标签: javascript typescript object

我有这样的对象

    var RecuiterInfo = {}
    RecruiterInfo.Quote: "1"
    RecruiterInfo.CompanyName: "Imperial Innovations",
    RecruiterInfo.OrganizationSize: "2",
    RecruiterInfo.Person.CitizanZenship: null,
    RecruiterInfo.Person.DOB: "Farkhonda@hotmail.com",
    RecruiterInfo.Person.Email: "Farkhonda",
    RecruiterInfo.Person.FirstName: "FaiZi",
    RecruiterInfo.Person.Gender: "4456565656"
    RecruiterInfo.Person.LastName: "4456565656",
    RecruiterInfo.Person.PhoneNo: "4456565656",
    RecruiterInfo.Person.UserId.UserName: "MTIzNDU2",
    RecruiterInfo.Person.UserId.Password: null

我想将此对象转换为 这种特定的格式,如

        {
      "QueryObjectID":"RecruiterInfo",
      "Values": [
        {
          "AppFieldID": "Person",
          "Value": [
            {
              "AppFieldID": "CitizanZenship",
              "Value": "1"
            },
            {
              "AppFieldID": "DOB",
              "Value": "01/01/2019"
            },
            {
              "AppFieldID": "Email",
              "Value": "test1@test.com"
            },
            {
              "AppFieldID": "FirstName",
              "Value": "test"
            },
            {
              "AppFieldID": "Gender",
              "Value": "1"
            },
            {
              "AppFieldID": "LastName",
              "Value": "test last"
            },
            {
              "AppFieldID": "PhoneNo",
              "Value": "7897897895"
            },
            {
              "AppFieldID": "UserId",
              "Value": [
                {
                  "AppFieldID": "UserName",
                  "Value": "test1@test.com"
                },
                {
                  "AppFieldID": "Password",
                  "Value": "123456"
                }
              ]
            }
          ]
        },
        {
          "AppFieldID": "Quote",
          "Value": "Hi Im test"
        },
        {
          "AppFieldID": "CompanyName",
          "Value": "Terst"
        }
      ]
    }

,但是应该是通用/动态的(我在这里不需要任何静态的东西,因为在功能中会有多个点/或嵌套的对象) 我认为循环将基于。(点),并且将嵌套

有人可以帮我进行此json转换

预先感谢

2 个答案:

答案 0 :(得分:2)

您可以递归进行。

const convert = (obj) => {
  if (obj === null) return null
  if (typeof obj !== 'object') return obj
  return Object.entries(obj)
    .reduce((acc, [key, value]) => acc.concat({
      AppFieldId: key,
      Value: convert(value), // Recursive call
    }), [])
}

console.log(JSON.stringify(convert(RecruiterInfo), null, 2))

并非完全如您所愿,但是您首先需要静态内容。

[
  {
    "AppFieldId": "Quote",
    "Value": "1"
  },
  {
    "AppFieldId": "CompanyName",
    "Value": "Imperial Innovations"
  },
  {
    "AppFieldId": "OrganizationSize",
    "Value": "2"
  },
  {
    "AppFieldId": "Person",
    "Value": [
      {
        "AppFieldId": "CitizanZenship",
        "Value": null
      },
      {
        "AppFieldId": "DOB",
        "Value": "Farkhonda@hotmail.com"
      },
      {
        "AppFieldId": "Email",
        "Value": "Farkhonda"
      },
      {
        "AppFieldId": "FirstName",
        "Value": "FaiZi"
      },
      {
        "AppFieldId": "Gender",
        "Value": "4456565656"
      },
      {
        "AppFieldId": "LastName",
        "Value": "4456565656"
      },
      {
        "AppFieldId": "PhoneNo",
        "Value": "4456565656"
      },
      {
        "AppFieldId": "UserId",
        "Value": [
          {
            "AppFieldId": "UserName",
            "Value": "MTIzNDU2"
          },
          {
            "AppFieldId": "Password",
            "Value": null
          }
        ]
      }
    ]
  }
]

答案 1 :(得分:1)

您可以采用迭代方法。

var data = { 'RecruiterInfo.Quote': "1", 'RecruiterInfo.CompanyName': "Imperial Innovations", 'RecruiterInfo.OrganizationSize': "2", 'RecruiterInfo.Person.CitizanZenship': null, 'RecruiterInfo.Person.DOB': "Farkhonda@hotmail.com", 'RecruiterInfo.Person.Email': "Farkhonda", 'RecruiterInfo.Person.FirstName': "FaiZi", 'RecruiterInfo.Person.Gender': "4456565656", 'RecruiterInfo.Person.LastName': "4456565656", 'RecruiterInfo.Person.PhoneNo': "4456565656", 'RecruiterInfo.Person.UserId.UserName': "MTIzNDU2", 'RecruiterInfo.Person.UserId.Password': null },
    result = Object.entries(data).reduce((r, [s, v]) => {
        var path = s.split('.'),
            last = path.pop();

        path
            .reduce((o, k, i) => {
                var temp = o.find(q => q[i ? 'AppFieldID' : 'QueryObjectID'] === k);
                if (!temp) o.push(temp = i ? { AppFieldID: k, Value: [] } : { QueryObjectID: k, Values: [] });
                return temp[i ? 'Value' : 'Values'];
            }, r)
            .push({ AppFieldID: last, Value: v });

        return r;
    }, []);

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