javascript - 使用示例数据重新排列 json

标签 javascript node.js json node-modules

原始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 数据所示?

最佳答案

最简单的方法是使用 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);

关于javascript - 使用示例数据重新排列 json,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57152453/

相关文章:

javascript - HTML onclick 函数不通过 PHP echo 调用

javascript - 是否可以使用 CSS 或 Javascript 隐藏网页中的光标?

node.js - npm 开发 :start does not run

node.js - 使用 Node 运行 .vbs 脚本

json - 如何用各种数组解析json

mysql - 使用 JSON 而不是规范化数据,这种方法是否正确?

javascript - 我们应该更喜欢 Chai 的 `to.be.an(' undefined' )` vs ` 到 .equal(undefined)` 吗?

javascript - 如何使用sttrek/scrollMonitor的方法 'one'?

node.js - 将 ES6 "import"转换为 nodejs "require"的正确方法

c# - 将对象列表以 json 格式保存到 blob