不确定问题标题是否是自描述的,但这里是问题陈述:我想将多级JSON转换为多级分组json 。我们的想法是使用这个JSON数据来创建手风琴。有没有可以帮助我的图书馆?
提前致谢。
JSON我有 -
[
{
"Assessment": {
"AssessmentId": 3,
"AssessmentText": "CRM",
"Measures": {
"MeasureId": 6,
"MeasureText": "CF",
"SubMeasures": {
"SubMeasureId": 12,
"SubMeasureText": "TNCF",
"Apps": {
"Name": "BOT",
"Rating": 0
}
}
}
}
},
{
"Assessment": {
"AssessmentId": 3,
"AssessmentText": "CRM",
"Measures": {
"MeasureId": 6,
"MeasureText": "CF",
"SubMeasures": {
"SubMeasureId": 12,
"SubMeasureText": "TNCF",
"Apps": {
"Name": "CPSM",
"Rating": 0
}
}
}
}
},
{
"Assessment": {
"AssessmentId": 3,
"AssessmentText": "CRM",
"Measures": {
"MeasureId": 6,
"MeasureText": "CF",
"SubMeasures": {
"SubMeasureId": 12,
"SubMeasureText": "TNCF",
"Apps": {
"Name": "DEA",
"Rating": 0
}
}
}
}
},
{
"Assessment": {
"AssessmentId": 3,
"AssessmentText": "CRM",
"Measures": {
"MeasureId": 8,
"MeasureText": "DEV",
"SubMeasures": {
"SubMeasureId": 16,
"SubMeasureText": "CICD",
"Apps": {
"Name": "BOT",
"Rating": 0
}
}
}
}
},
{
"Assessment": {
"AssessmentId": 3,
"AssessmentText": "CRM",
"Measures": {
"MeasureId": 8,
"MeasureText": "DEV",
"SubMeasures": {
"SubMeasureId": 16,
"SubMeasureText": "CICD",
"Apps": {
"Name": "CPSM",
"Rating": 0
}
}
}
}
},
{
"Assessment": {
"AssessmentId": 3,
"AssessmentText": "CRM",
"Measures": {
"MeasureId": 8,
"MeasureText": "DEV",
"SubMeasures": {
"SubMeasureId": 16,
"SubMeasureText": "CICD",
"Apps": {
"Name": "DEA",
"Rating": 0
}
}
}
}
}
]
我想要什么 -
[
{
"Assessment": {
"AssessmentId": 3,
"AssessmentText": "CRM",
"Measures": {
"MeasureId": 6,
"MeasureText": "CF",
"SubMeasures": {
"SubMeasureId": 12,
"SubMeasureText": "TNCF",
"Apps": [{
"Name": "CPSM",
"Rating": 0
},{
"Name": "DEA",
"Rating": 0
},{
"Name": "BOT",
"Rating": 0
}]
}
}
}
},{
"Assessment": {
"AssessmentId": 3,
"AssessmentText": "CRM",
"Measures": {
"MeasureId": 8,
"MeasureText": "DEV",
"SubMeasures": {
"SubMeasureId": 16,
"SubMeasureText": "CICD",
"Apps": [{
"Name": "BOT",
"Rating": 0
},{
"Name": "CPSM",
"Rating": 0
},{
"Name": "DEA",
"Rating": 0
}]
}
}
}
}
]
答案 0 :(得分:1)
您可以将哈希表作为对同一AssessmentId
和MeasureId
的引用。
如果不存在散列,则使用实际副本(没有原始数据的变异)构建新对象,并将Apps
值转换为数组。将临时opbejct推送到结果数组,并保持对哈希表中Apps
数组的引用。
如果找到已知的哈希,则只需更新数组。
var data = [{ Assessment: { AssessmentId: 3, AssessmentText: "CRM", Measures: { MeasureId: 6, MeasureText: "CF", SubMeasures: { SubMeasureId: 12, SubMeasureText: "TNCF", Apps: { Name: "BOT", Rating: 0 } } } } }, { Assessment: { AssessmentId: 3, AssessmentText: "CRM", Measures: { MeasureId: 6, MeasureText: "CF", SubMeasures: { SubMeasureId: 12, SubMeasureText: "TNCF", Apps: { Name: "CPSM", Rating: 0 } } } } }, { Assessment: { AssessmentId: 3, AssessmentText: "CRM", Measures: { MeasureId: 6, MeasureText: "CF", SubMeasures: { SubMeasureId: 12, SubMeasureText: "TNCF", Apps: { Name: "DEA", Rating: 0 } } } } }, { Assessment: { AssessmentId: 3, AssessmentText: "CRM", Measures: { MeasureId: 8, MeasureText: "DEV", SubMeasures: { SubMeasureId: 16, SubMeasureText: "CICD", Apps: { Name: "BOT", Rating: 0 } } } } }, { Assessment: { AssessmentId: 3, AssessmentText: "CRM", Measures: { MeasureId: 8, MeasureText: "DEV", SubMeasures: { SubMeasureId: 16, SubMeasureText: "CICD", Apps: { Name: "CPSM", Rating: 0 } } } } }, { Assessment: { AssessmentId: 3, AssessmentText: "CRM", Measures: { MeasureId: 8, MeasureText: "DEV", SubMeasures: { SubMeasureId: 16, SubMeasureText: "CICD", Apps: { Name: "DEA", Rating: 0 } } } } }],
hash = Object.create(null),
result = [];
data.forEach(function (o) {
var key = [o.Assessment.AssessmentId, o.Assessment.Measures.MeasureId].join('|'),
temp;
if (hash[key]) {
hash[key].push(o.Assessment.Measures.SubMeasures.Apps);
return;
}
hash[key] = [o.Assessment.Measures.SubMeasures.Apps];
temp = JSON.parse(JSON.stringify(o));
temp.Assessment.Measures.SubMeasures.Apps = hash[key];
result.push(temp);
});
console.log(result);

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

答案 1 :(得分:0)
如果所有Assessment
个对象具有相同的AssessmentId
,您可以对这些数据使用 .reduce()
method :
var result = data.reduce(function(s, d, idx){
if(idx==0){
let apps = [];
apps.push(s.Assessment.Measures.SubMeasures.Apps);
s.Assessment.Measures.SubMeasures.Apps = apps;
}else{
s.Assessment.Measures.SubMeasures.Apps.push(d.Assessment.Measures.SubMeasures.Apps);
}
return s;
}, data[0]);
<强>解释强>
使用result
数组中的第一项初始化data
对象,使用data[0]
并通过.reduce()
调用传递,并将其Assessment.Measures.SubMeasures.Apps
转换为array
然后在每次迭代时推送Assessment.Measures.SubMeasures.Apps
上的所有var data = [{
"Assessment": {
"AssessmentId": 3,
"AssessmentText": "CRM",
"Measures": {
"MeasureId": 6,
"MeasureText": "CF",
"SubMeasures": {
"SubMeasureId": 12,
"SubMeasureText": "TNCF",
"Apps": {
"Name": "BOT",
"Rating": 0
}
}
}
}
},
{
"Assessment": {
"AssessmentId": 3,
"AssessmentText": "CRM",
"Measures": {
"MeasureId": 6,
"MeasureText": "CF",
"SubMeasures": {
"SubMeasureId": 12,
"SubMeasureText": "TNCF",
"Apps": {
"Name": "CPSM",
"Rating": 0
}
}
}
}
},
{
"Assessment": {
"AssessmentId": 3,
"AssessmentText": "CRM",
"Measures": {
"MeasureId": 6,
"MeasureText": "CF",
"SubMeasures": {
"SubMeasureId": 12,
"SubMeasureText": "TNCF",
"Apps": {
"Name": "DEA",
"Rating": 0
}
}
}
}
}
];
var result = data.reduce(function(s, d, idx){
if(idx==0){
let apps = [];
apps.push(s.Assessment.Measures.SubMeasures.Apps);
s.Assessment.Measures.SubMeasures.Apps = apps;
}else{
s.Assessment.Measures.SubMeasures.Apps.push(d.Assessment.Measures.SubMeasures.Apps);
}
return s;
}, data[0]);
console.log(result);
。
<强>演示:强>
SubMeausrement
好吧,如果你有多个array
个对象,并且想要在var result = {};
data.forEach(function(d, idx) {
if (idx == 0) {
let s = d;
let subMeasures = [];
let apps = [];
apps.push(d.Assessment.Measures.SubMeasures.Apps);
s.Assessment.Measures.SubMeasures.Apps = apps;
subMeasures.push(d.Assessment.Measures.SubMeasures);
s.Assessment.Measures.SubMeasures = subMeasures;
result = Object.assign({}, s);
} else {
let found = elemExists(result.Assessment.Measures.SubMeasures, "SubMeasureId", d.Assessment.Measures.SubMeasures.SubMeasureId);
if (found >= 0) { result.Assessment.Measures.SubMeasures[found].Apps.push(d.Assessment.Measures.SubMeasures.Apps);
} else { result.Assessment.Measures.SubMeasures.push(d.Assessment.Measures.SubMeasures);
}
}
});
中对它们进行分组,那么你可以试试这个代码:
elemExists(array, property, val)
其中element
是一个util函数,用于检查array
中是否存在property
,使用特定的objects
来比较迭代的index
并返回{ {1}}中的object
{1}}。{/}
<强>演示:强>
array