gpt4 book ai didi

javascript - 在 JavaScript 中操作 JSON 和组键

转载 作者:行者123 更新时间:2023-11-30 07:52:06 26 4
gpt4 key购买 nike

我正在尝试为我的 JSON 获取某种结构,但我在这个过程中遇到困难并陷入无限循环。

我设法通过使用 reduce() 方法对原始文件进行了处理,但我坚持最后一步。

这是 JSON:

{
"someNumbers_like_15269": {
"someNumbers_like_35896": [{
"TestPoint": "A",
"ItemNo": "12110",
"TestDesc": "Penalty Points"
}, {
"TestPoint": "A",
"ItemNo": "12110",
"TestDesc": "Brix"
}, {
"TestPoint": "B",
"ItemNo": "12110",
"TestDesc": "Texture"
}, {
"TestPoint": "B",
"ItemNo": "12110",
"TestDesc": "Viscosity"
}, {
"TestPoint": "C",
"ItemNo": "12110",
"TestDesc": "PH"
}, {
"TestPoint": "C",
"ItemNo": "12110",
"TestDesc": "Taste"
}]
}
}

以及想要的结果

{
"someNumbers_like_15269": {
"someNumbers_like_35896": {
"ItemNo": "12110",
"someOtherThing": "blabla",
"Test": [{
"TestPoint": "A",
"TestDesc": ["Penalty Points", "Brix"]
}, {
"TestPoint": "B",
"TestDesc": ["Texture", "Viscosity"]
}, {
"TestPoint": "C",
"TestDesc": ["PH", "Taste"]
}]
}
}

}附注:会有多个CC多个Batch,JSON 85000多行

**

我原来的尝试可以在here中找到

**

最佳答案

使用Array.reduce , Array.forEach , Object.valuesObject.entries

var data = {"someNumbers_like_15269":{"someNumbers_like_35896":[{"TestPoint":"A","ItemNo":"12110","TestDesc":"Penalty Points"},{"TestPoint":"A","ItemNo":"12110","TestDesc":"Brix"},{"TestPoint":"B","ItemNo":"12110","TestDesc":"Texture"},{"TestPoint":"B","ItemNo":"12110","TestDesc":"Viscosity"},{"TestPoint":"C","ItemNo":"12110","TestDesc":"PH"},{"TestPoint":"C","ItemNo":"12110","TestDesc":"Taste"}],"someNumbers_like_21":[{"TestPoint":"A","ItemNo":"12110","TestDesc":"Penalty Points"},{"TestPoint":"A","ItemNo":"12110","TestDesc":"Brix"},{"TestPoint":"B","ItemNo":"12110","TestDesc":"Texture"},{"TestPoint":"B","ItemNo":"12110","TestDesc":"Viscosity"},{"TestPoint":"C","ItemNo":"12110","TestDesc":"PH"},{"TestPoint":"C","ItemNo":"12110","TestDesc":"Taste"}]},"someNumbers_like_15270":{"someNumbers_like_35800":[{"TestPoint":"A","ItemNo":"12110","TestDesc":"Penalty Points"},{"TestPoint":"A","ItemNo":"12110","TestDesc":"Brix"},{"TestPoint":"B","ItemNo":"12110","TestDesc":"Texture"},{"TestPoint":"B","ItemNo":"12110","TestDesc":"Viscosity"},{"TestPoint":"C","ItemNo":"12110","TestDesc":"PH"},{"TestPoint":"C","ItemNo":"12110","TestDesc":"Taste"}]}};

// Iterate for all first level values
Object.values(data).forEach(v => {
// Iterate for all second level values
Object.entries(v).forEach(([k,v1]) => {
// Set the updated value back in object
v[k] = Object.values(v1.reduce((a,{TestDesc, TestPoint, ...rest}) => {
a[TestPoint] ? a[TestPoint].TestDesc.push(TestDesc) : a[TestPoint] = {TestPoint, ...rest, TestDesc : [TestDesc]};
return a;
}, {}));
});
})

console.log(data);

编辑

var data = {"someNumbers_like_15269":{"someNumbers_like_35896":[{"TestPoint":"A","ItemNo":"12110","TestDesc":"Penalty Points"},{"TestPoint":"A","ItemNo":"12110","TestDesc":"Brix"},{"TestPoint":"B","ItemNo":"12110","TestDesc":"Texture"},{"TestPoint":"B","ItemNo":"12110","TestDesc":"Viscosity"},{"TestPoint":"C","ItemNo":"12110","TestDesc":"PH"},{"TestPoint":"C","ItemNo":"12110","TestDesc":"Taste"}],"someNumbers_like_21":[{"TestPoint":"A","ItemNo":"12110","TestDesc":"Penalty Points"},{"TestPoint":"A","ItemNo":"12110","TestDesc":"Brix"},{"TestPoint":"B","ItemNo":"12110","TestDesc":"Texture"},{"TestPoint":"B","ItemNo":"12110","TestDesc":"Viscosity"},{"TestPoint":"C","ItemNo":"12110","TestDesc":"PH"},{"TestPoint":"C","ItemNo":"12110","TestDesc":"Taste"}]},"someNumbers_like_15270":{"someNumbers_like_35800":[{"TestPoint":"A","ItemNo":"12110","TestDesc":"Penalty Points"},{"TestPoint":"A","ItemNo":"12110","TestDesc":"Brix"},{"TestPoint":"B","ItemNo":"12110","TestDesc":"Texture"},{"TestPoint":"B","ItemNo":"12110","TestDesc":"Viscosity"},{"TestPoint":"C","ItemNo":"12110","TestDesc":"PH"},{"TestPoint":"C","ItemNo":"12110","TestDesc":"Taste"}]}};

// Iterate for all first level values
Object.values(data).forEach(v => {
// Iterate for all second level values
Object.entries(v).forEach(([k,v1]) => {
let {TestDesc, TestPoint, ...rest} = v1[0];
let Test = Object.values(v1.reduce((a,{TestDesc, TestPoint}) => {
a[TestPoint] ? a[TestPoint].TestDesc.push(TestDesc) : a[TestPoint] = {TestPoint, TestDesc : [TestDesc]};
return a;
}, {}));
v[k] = {...rest, Test};
});
})

console.log(data);

关于javascript - 在 JavaScript 中操作 JSON 和组键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50756849/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com