javascript - 属性未显示在 json 中,重构 json 对象

标签 javascript node.js json node-modules

原始json数据:

{
  "UniversalOne": "",
  "CommonOne": "",
  "Implementations": [
    {
      "BirthDate": "",
      "UniqueTraits": "",
      "Male": {
        "Gender": "Male",
        "PlaceOfBirth": "",
        "Weight": "",
        "Height": "",
        "EyeColor": ""
      },
      "Female": {
        "Gender": "Female",
        "PlaceOfBirth": "",
        "Weight": "",
        "Height": "",
        "EyeColor": ""
      },
      "Country": [
        {
          "Orientation": "Male",
          "Name": "ABCD",
          "County": "East"
        },
        {
          "Orientation": "Male",
          "Name": "ABCD",
          "County": "West"
        },
        {
          "Orientation": "Female",
          "Name": "EFGH",
          "County": "East"
        },
        {
          "Orientation": "Female",
          "Name": "EFGH",
          "County": "West"
        },
        {
          "Orientation": "Female",
          "Name": "IJKL"
        }
      ],
      "State": [
        {
          "Address": "XYZ Street",
          "ZipCode": "US"
        }
      ],
      "Boy": [
        {
          "AgeGroup": "A",
          "Id": 1,
          "MaternalName": "",
          "PaternalName": ""
        },
        {
          "AgeGroup": "B",
          "Id": 2,
          "MaternalName": "",
          "PaternalName": ""
        },
        {
          "AgeGroup": "C",
          "Id": 3,
          "MaternalName": "",
          "PaternalName": ""
        }
      ]
    }
  ],
  "PersonalityTraits": [
    {
      "Type": "Positive"
    },
    {
      "Type": "Negative"
    }
  ],
  "UniversalTwo": "",
  "CommonTwo": "",
  "EatingHabits": {
    "Type": "Excessive"
  },
  "ReadingHabits": {
    "Type": "Fast"
  },
  "FitnessHabits": {},
  "UniversalThree": "",
  "CommonThree": ""
}

预期的 json 响应:

  {
  "UniversalOne": "",
  "CommonOne": "",
  "Implementations": [
    {
      "BirthDate": "",
      "UniqueTraits": "",
      "Male": {
        "Gender": "Male",
        "PlaceOfBirth": "",
        "Weight": "",
        "Height": "",
        "EyeColor": "",
        "Country": [
          {
            "Orientation": "Male",
            "Name": "ABCD"
          }
        ],
        "EastCounty": {
          "Orientation": "Male",
          "Name": "ABCD",
          "County": "East"
        },
        "State": [
          {
            "Address": "XYZ Street",
            "ZipCode": "US"
          }
        ]
      },
      "Female": {
        "Gender": "Female",
        "PlaceOfBirth": "",
        "Weight": "",
        "Height": "",
        "EyeColor": "",
        "Country": [
          {
            "Orientation": "Female",
            "Name": "EFGH"
          },
          {
            "Orientation": "Female",
            "Name": "IJKL"
          }
        ],
        "EastCounty": {
          "Orientation": "Female",
          "Name": "EFGH",
          "County": "East"
        },
        "State": [
          {
            "Address": "XYZ Street",
            "ZipCode": "US"
          }
        ]
      },
      "Girl": [
        {
          "AgeGroup": "A",
          "identification": [
            {
              "Number": 1,
              "MaternalName": "",
              "PaternalName": ""
            }
          ]
        },
        {
          "AgeGroup": "B",
          "identification": [
            {
              "Number": 1,
              "MaternalName": "",
              "PaternalName": ""
            }
          ]
        },
        {
          "AgeGroup": "C",
          "identification": [
            {
              "Number": 1,
              "MaternalName": "",
              "PaternalName": ""
            }
          ]
        }
      ]
    }
  ],
  "PersonalityTraits": [
    {
      "Type": "Positive"
    },
    {
      "Type": "Negative"
    }
  ],
  "UniversalTwo": "",
  "CommonTwo": "",
  "EatingHabits": {
    "Type": "Excessive"
  },
  "ReadingHabits": {
    "Type": "Fast"
  },
  "FitnessHabits": {},
  "UniversalThree": "",
  "CommonThree": ""
}

问题:

我有三个具体问题:

1) 如何保留直接在“男性”和“女性”之下以及“男性”之前的属性?在我运行程序后,这些属性不会显示在我的响应中。

我想保留像

这样的属性
"BirthDate":"", 
"UniqueTraits": "" AND 

"Gender": "Male", 
"PlaceOfBirth": "", 
"Weight": "", 
"Height": "", 
"EyeColor": "" 

与我原来和预期的 json 数据完全一样。

2) 如何根据“County”在 Male 和 Female 中的 Country[] 之后添加另一个 EastCounty{}:East and Orientation?请引用原始和预期的json。

3) 如何将原始 json 中的 Boy[] 重组为与预期 json 响应中 Girl[] 中所示完全相同的新结构?请注意,Boy[] 中的“Id”在 Girl 中变为“Number”。因此,如果任一“AgeGroup”中有多个“identification”,则每条记录的“Number”都会依次更改。

当前程序:

function modifyImplementations(Implementations) {
  var finalResult = [];

  for (var i = 0; i < Implementations.Implementations.length; i++) {
    var currentImplementation = Implementations.Implementations[i];
    var targetObj = {
      "Male": {
        "Gender": "Male",
        "Country": [],
        "State": currentImplementation.State
      },
      "Female": {
        "Gender": "Female",
        "Country": [],
        "State": currentImplementation.State
      }
    };

    for (var j = 0; j < currentImplementation.Country.length; j++) {
      var currentCountry = currentImplementation.Country[j];
      if (currentCountry.Orientation === 'Male') {
        targetObj.Male.Country.push(currentCountry);
      } else if (currentCountry.Orientation === 'Female') {
        targetObj.Female.Country.push(currentCountry);
      }
    }
    finalResult.push(targetObj);
  }
  return finalResult
}

var x = Object.assign({}, Implementations);
x.Implementations = modifyImplementations(Implementations);

console.log(JSON.stringify(x));

最佳答案

这应该是可以产生预期结果的工作函数,请进行一些重构,因为在代码的少数区域肯定有更好的方法来实现,只需在这里快速完成您的所有需求以产生预期的 o/p 以及您的问题需求用有效的 JSON 更新:

function modifyImplementations(Implementations) {

    for (let i = 0; i < Implementations.Implementations.length; i++) {
        let currentImplementation = Implementations.Implementations[i];

        currentImplementation['Male']['Country'] = []
        currentImplementation['Female']['Country'] = []
        currentImplementation['Male']['EastCounty'] = []
        currentImplementation['Female']['EastCounty'] = []
        currentImplementation['Male']['State'] = currentImplementation['State'];
        currentImplementation['Female']['State'] = currentImplementation['State'];

        for (let j = 0; j < currentImplementation.Country.length; j++) {
            let currentCountry = currentImplementation.Country[j];
            let currentCountryObj = {}
            if (currentCountry.Orientation === 'Male') {
                if (currentCountry.County && currentCountry.County == "East") {
                    currentCountryObj['County'] = currentCountry.County
                    currentCountryObj['Name'] = currentCountry.Name
                    currentCountryObj['Orientation'] = currentCountry.Orientation
                    currentImplementation['Male']['EastCounty'].push(currentCountryObj)
                } else {
                    currentCountryObj['Name'] = currentCountry.Name
                    currentCountryObj['Orientation'] = currentCountry.Orientation
                    currentImplementation['Male']['Country'].push(currentCountryObj);
                }
            } else if (currentCountry.Orientation === 'Female') {
                if (currentCountry.County && currentCountry.County == "East") {
                    currentCountryObj['County'] = currentCountry.County
                    currentCountryObj['Name'] = currentCountry.Name
                    currentCountryObj['Orientation'] = currentCountry.Orientation
                    currentImplementation['Female']['EastCounty'].push(currentCountryObj)
                } else {
                    currentCountryObj['Name'] = currentCountry.Name
                    currentCountryObj['Orientation'] = currentCountry.Orientation
                    currentImplementation['Female']['Country'].push(currentCountryObj);
                }
            }
        }
        delete currentImplementation['Country']
        delete currentImplementation['State']

        let mapObj = [];
        for (items of currentImplementation.Boy) {
            let objs = currentImplementation.Boy.filter((obj) => {
                return items.AgeGroup === obj.AgeGroup
            })
            mapObj.push(objs)
            currentImplementation.Boy = currentImplementation.Boy.filter(e => e.AgeGroup !== items.AgeGroup);
        }

        let finalArray = mapObj.filter(e => e.length > 0)
        currentImplementation['Girl'] = []
        for (anArray of finalArray) {
            let finalObj = {}
            finalObj.identification = [];
            if (anArray.length && anArray.length > 1) {
                let number = 1
                for (oneObj of anArray) {
                    let objs = {};
                    objs['Number'] = number
                    objs['MaternalName'] = oneObj['MaternalName']
                    objs['PaternalName'] = oneObj['PaternalName']
                    number += 1
                    finalObj['AgeGroup'] = oneObj.AgeGroup
                    finalObj.identification.push(objs);
                }
            } else if (anArray.length == 1) {
                let objs = {};
                finalObj['AgeGroup'] = anArray[0].AgeGroup
                objs = {};
                objs['Number'] = 1
                objs['MaternalName'] = anArray[0]['MaternalName']
                objs['PaternalName'] = anArray[0]['PaternalName']
                finalObj.identification.push(objs);
            }
            currentImplementation['Girl'].push(finalObj)
            delete currentImplementation['Boy']
        }
    }
    return Implementations
}

关于javascript - 属性未显示在 json 中,重构 json 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57169899/

相关文章:

javascript - 如何在 jQuery.css() 方法中为所有浏览器编写 CSS 代码?

javascript - meteor 应用程序无法在模数上工作

javascript - Vue 原型(prototype) Axios

node.js - 如何使用 RequireJS 运行 jasmine-node 测试

javascript - 在 Node 中使用 'return {value}' 是否合适?

javascript - 将 json 对象作为 'payload' 传递给 db -rails postgres

javascript - 如何控制WordPress中视频播放器的大小

javascript - Nodejs回调过程

c++ - 如何让 boost json 使用正确的数据类型

javascript - 在 JavaScript 中获取 JSON 对象的最后一个元素