【问题标题】:Rearrange json with sample data使用示例数据重新排列 json
【发布时间】:2019-11-30 19:25:18
【问题描述】:

原始json数据:

{
  "UniversalOne": "",
  "CommonOne": ""
  "Implementations": [
    {      
      "Male": {
        "Gender": "Male"              
      },
      "Female": {
        "Gender": "Female"       
      },

      "Country": [
        {
          "Orientation": "Male",          
          "Name": ABCD
        },
        {
          "Orientation": "Female",
          "Name": EFGH
        },
        {
          "Orientation": "Female",
          "Name": IJKL        
        }
      ],
      "State": [
        {          
          "Address": "XYZ Street",
          "ZipCode": "US"
        }
      ]
    }
  ],
  "PersonalityTraits": [
    {
      "Type": "Positive"
    },
    {      
      "Type": "Negative" 
    }  
  ],
  "UniversalTwo": "",  
  "CommonTwo": "",  
  "EatingHabits": {    
    "Type": "Excessive"
  },
  "ReadingHabits": {    
    "Type": "Fast"    
  },
  "FitnessHabits": {   
  },
  "UniversalThree": "",
  "CommonThree": ""
}

预期的 json 数据:

{ 
  "UniversalOne": "",
  "CommonOne": ""
  "Implementations":    [
    {      
      "Male": {
        "Gender": "Male"         
         "Country": [
                {
                  "Orientation": "Male",          
                  "Name": ABCD
                }
            ],
              "State": [
                {          
                  "Address": "XYZ Street",
                  "ZipCode": "US"
                }
            ]

      },
      "Female": {
        "Gender": "Female"          
        "Country": [
                {
                  "Orientation": "Female",
                  "Name": EFGH
                },
                {
                  "Orientation": "Female",
                  "Name": IJKL        
                }
              ],
        "State": [
                {          
                  "Address": "XYZ Street",
                  "ZipCode": "US"
                }
            ]
        }
    }
  ],
  "PersonalityTraits": [
    {
      "Type": "Positive"
    },
    {      
      "Type": "Negative" 
    }  
  ],
  "UniversalTwo": "",  
  "CommonTwo": "",  
  "EatingHabits": {    
    "Type": "Excessive"
  },
  "ReadingHabits": {    
    "Type": "Fast"    
  },
  "FitnessHabits": {   
  },
  "UniversalThree": "",
  "CommonThree": ""

}

程序:

//Original JSON data in question.
var Implementations = {
  "UniversalOne": "",
  "CommonOne": ""
  "Implementations": [
    {      
      "Male": {
        "Gender": "Male"              
      },
      "Female": {
        "Gender": "Female"       
      },

      "Country": [
        {
          "Orientation": "Male",          
          "Name": ABCD
        },
        {
          "Orientation": "Female",
          "Name": EFGH
        },
        {
          "Orientation": "Female",
          "Name": IJKL        
        }
      ],
      "State": [
        {          
          "Address": "XYZ Street",
          "ZipCode": "US"
        }
      ]
    }
  ],
  "PersonalityTraits": [
    {
      "Type": "Positive"
    },
    {      
      "Type": "Negative" 
    }  
  ],
  "UniversalTwo": "",  
  "CommonTwo": "",  
  "EatingHabits": {    
    "Type": "Excessive"
  },
  "ReadingHabits": {    
    "Type": "Fast"    
  },
  "FitnessHabits": {   
  },
  "UniversalThree": "",
  "CommonThree": ""
}

// Program that make the conversion
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);
}

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

如预期的 json 数据所示,如何在 Implementations 对象之外添加诸如 Personality Traits、Eating Habits、Reading Habits、Fitness Habits 和 Universal 和 common 等属性?

【问题讨论】:

  • 请同时发布预期结果。您希望对象重新排列成什么?
  • 我已经进行了编辑以使其清楚。谢谢!

标签: javascript node.js json node-modules


【解决方案1】:

最简单的方法是使用Object.assign 来合并属性。

//The Original Data
const Implementations = {
  "Implementations": [
    {
      //Ignore 
    }
  ]
}
//The Attributes needed
const attributes = {
  "UniversalOne": "",
  "CommonOne": "",
  "PersonalityTraits": [
    {
      "Type": "Positive"
    },
    {
      "Type": "Negative"
    }
  ],
  "UniversalTwo": "",
  "CommonTwo": "",
  "EatingHabits": {
    "Type": "Excessive"
  },
  "ReadingHabits": {
    "Type": "Fast"
  },
  "FitnessHabits": {
  },
  "UniversalThree": "",
  "CommonThree": ""
}

const newData = Object.assign({}, Implementations, attributes);
console.dir(newData);

或者只是在里面添加数据。

const Implementations = {
  "Implementations": [
    {
      //Ignore 
    }
  ]
}

const newData = {
  "UniversalOne": "",
  "CommonOne": "",
  "PersonalityTraits": [
    {
      "Type": "Positive"
    },
    {
      "Type": "Negative"
    }
  ],
  "UniversalTwo": "",
  "CommonTwo": "",
  "EatingHabits": {
    "Type": "Excessive"
  },
  "ReadingHabits": {
    "Type": "Fast"
  },
  "FitnessHabits": {
  },
  "UniversalThree": "",
  "CommonThree": ""
}

newData.Implementations = Implementations.Implementations;
console.dir(newData);

【讨论】:

  • 感谢您的回复。您能否在不添加另一个变量并将两者合并的情况下帮助编辑程序?我想推送有问题的对象而不是合并。当前程序没有解决诸如个性特征、饮食、阅读习惯之类的对象以及实现之外的通用和常见属性。
  • 您在编辑后完全改变了问题。请提出一个单独的问题。抱歉,我不会再编辑我的答案了。
  • 好的,我将添加一个单独的问题。如果我们必须合并两个 json 对象,您的解决方案仍然有效。
猜你喜欢
  • 2019-04-13
  • 2022-11-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-07-16
  • 2010-09-20
  • 2018-02-06
相关资源
最近更新 更多