[英]Nested Array in JSON Objects Conversion
I'm struggling with converting the nested JSON array that I have. 我正在努力转换我拥有的嵌套JSON数组。
{
"Id": "1234",
"Company": {
"element": [{
"Name": "htc",
"Contacts": {
"element": [{
"name": "john",
"phone": "1234"
}, {
"name": "peter",
"phone": "5678"
}]
},
"Address": {
"element": {
"country": "us",
"state": "cali"
}
}
}, {
"Name": "samsung",
"Contacts": {
"element": [{
"name": "luke",
"phone": "0011"
}, {
"name": "james",
"phone": "2233"
}]
},
"Address": {
"element": {
"country": "us",
"state": "texas"
}
}
}]
}
}
As you'll notice, there's this "element" in the arrays "Company", "Contacts" and "Address". 正如您将注意到的,数组“公司”,“联系人”和“地址”中有“元素”。 But the output that I need to provide should not contain the "element" such as this code:
但是我需要提供的输出不应该包含“元素”,例如此代码:
{
"Id": "1234",
"Company": [{
"Name": "htc",
"Contacts": [{
"name": "john",
"phone": "1234"
}, {
"name": "peter",
"phone": "5678"
}],
"Address": [{
"country": "us",
"state": "cali"
}]
}, {
"Name": "samsung",
"Contacts": [{
"name": "luke",
"phone": "0011"
}, {
"name": "james",
"phone": "2233"
}],
"Address": [{
"country": "us",
"state": "texas"
}]
}]
}
I have no clue how to do in JavaScript. 我不知道如何在JavaScript中做。 Any ideas/tips are appreciate.
任何想法/提示都很感激。 Thank you
谢谢
The solution using Array.prototype.forEach()
function: 使用
Array.prototype.forEach()
函数的解决方案:
var companyData = { "Id": "1234", "Company": { "element": [{ "Name": "htc", "Contacts": { "element": [{ "name": "john", "phone": "1234" }, { "name": "peter", "phone": "5678" }] }, "Address": { "element": { "country": "us", "state": "cali" } } }, { "Name": "samsung", "Contacts": { "element": [{ "name": "luke", "phone": "0011" }, { "name": "james", "phone": "2233" }] }, "Address": { "element": { "country": "us", "state": "texas" } } }] } }; companyData.Company = companyData.Company.element; var omitElement = function(o){ if (!o['element']) return o; return (Array.isArray(o.element))? o.element : [o.element]; } companyData.Company.forEach(function (o) { o.Contacts = omitElement(o.Contacts); o.Address = omitElement(o.Address); }); console.log(companyData);
You can try something like this: 你可以尝试这样的事情:
var data={Id:"1234",Company:{element:[{Name:"htc",Contacts:{element:[{name:"john",phone:"1234"},{name:"peter",phone:"5678"}]},Address:{element:{country:"us",state:"cali"}}},{Name:"samsung",Contacts:{element:[{name:"luke",phone:"0011"},{name:"james",phone:"2233"}]},Address:{element:{country:"us",state:"texas"}}}]}}; var keysToClean = ["Address", "Contacts"] // Copy object instead of reference var result = Object.assign({}, data); result.Company = result.Company.element; result.Company.forEach(x => { keysToClean.forEach(k => { x[k] = Array.isArray(x[k]) ? x[k].element : [x[k].element] }) }) console.log(result);
Note: I have use Object.create
and Arrow functions
. 注意:我使用了
Object.create
和Arrow functions
。 They are not supported by old browsers. 旧浏览器不支持它们。 You can refer to following link for alternative to deep copy an object:
您可以参考以下链接来替代深层复制对象:
Please see this Plunker This should help.. it will generate desired result you need but be aware this is just a way to do this, and only meant for information purpose. 请参阅此Plunker这应该有帮助..它将产生您需要的所需结果,但请注意这只是一种方法,并且仅用于信息目的。 It's not production grade...
这不是生产等级......
function ParseData(data)
{
var newObject={Id:0, Company:[]};
newObject["Id"]=data["Id"];
newObject["Company"]=CreateCompanyObject(data["Company"]["element"]);
console.log(JSON.stringify(newObject));
}
function CreateCompanyObject(data)
{
var companies=[];
for(var i=0;i<data.length;i++)
{
companies.push({
name:data[i].Name,
contacts:CreateContactObject(data[i].Contacts.element),
Address:CreateAddressObject(data[i].Address.element)});
};
return companies;
}
function CreateContactObject(data){
var contacts=[];
for(var i=0;i<data.length;i++)
contacts.push(data[i]);
return contacts;
}
function CreateAddressObject(data){
var address=[];
if(typeof(data)=="array"){
for(var i=0;i<data.length;i++)
address.push(data[i]);
}
else
address.push(data);
return address;
}
You could check for element
and move the content a step ahead to its parent. 您可以检查
element
并将内容向前移动到其父级。
function deleteElement(object){ Object.keys(object).forEach(function (k) { if (object[k] && typeof object[k] === 'object') { if ('element' in object[k]) { object[k] = Array.isArray(object[k].element) ? object[k].element : [object[k].element]; } deleteElement(object[k]); } }); } var data = { Id: "1234", Company: { element: [{ Name: "htc", Contacts: { element: [{ name: "john", phone: "1234" }, { name: "peter", phone: "5678" }] }, Address: { element: { country: "us", state: "cali" } } }, { Name: "samsung", Contacts: { element: [{ name: "luke", phone: "0011" }, { name: "james", phone: "2233" }] }, Address: { element: { country: "us", state: "texas" } } }] } }; deleteElement(data); console.log(data);
.as-console-wrapper { max-height: 100% !important; top: 0; }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.