使用 JavaScript,我需要 groups.1
中的所有元素具有以下 value value :
{"schemas": "block"}
另外,我需要 groups.101
中的元素具有以下 value :
{"schemas": "all",
"native": "write"}
原来的 JSON 看起来像这样:
{
"revision": 76,
"groups": {
"1": {
"26": {
"schemas": "block"
},
"138": {
"schemas": "block"
},
"179": {
"schemas": "block"
},
"245": {
"schemas": "all",
"native": "write"
}
},
"101": {
"167": {
"schemas": "block"
}
}
}
}
预期结果如下所示
{
"revision": 76,
"groups": {
"1": {
"26": {
"schemas": "block"
},
"138": {
"schemas": "block"
},
"179": {
"schemas": "block"
},
"245": {
"schemas": "block"
}
},
"101": {
"167": {
"schemas": "all",
"native": "write"
}
}
}
}
回答1
const res = JSON.parse(JSON.stringify(json))
Object.keys(res.groups.1).forEach(key => res.groups.1[key] = {"schemas": "block"}
Object.keys(res.groups.101).forEach(key => res.groups.101[key] = {"schemas": "all",
"native": "write"}
是一个直接的解决方案,因为您从 json 对象开始,所以 JSON 序列化副本应该成功。
我们可以通过将路径拆分为数组并在“过滤器”步骤中应用它,然后应用函数来进行概括。
const filterTransformFactory = (filter, transform) => json => {
head = json
while(filter.length) {
head = head[filter.shift()]
}
Object.keys(head).forEach(key => transform(head, key))
}
const oneFilter = filterTransformFactory(['groups', 1], (o, key) => o[key] = {"schemas": "block"})
const oneooneFilter = filterTransformFactory(['groups', 101], (o, key) => o[key] = {"schemas": "all",
"native": "write"})
并将其应用于 json 的副本。通过提供一个 replaceTransform 函数,进一步重构可以使它相当漂亮:
const filterTransformFactory = (filter, transform) => json => {
head = json
while(filter.length) {
head = head[filter.shift()]
}
Object.keys(head).forEach(key => transform(head, key))
}
const replaceTransformFactory = payload => (o, key) => o[key] = payload
const oneTransform = replaceTransformFactory({"schemas": "block"})
const oneFilterTransform = filterTransformFactory(['groups', 1], oneTransform)
const oneooneTransform = replaceTransformFactory({"schemas": "all", "native": "write"})
const oneooneFilterTransform = filterTransformFactory(['groups', 101], oneooneTransform)
const result = JSON.parse(JSON.stringify(json))
oneFilterTransform(result)
oneooneFilterTransform(result)
回答2
我创建了一个配置对象,其中包含我们想要的所有组 transform 及其各自的 values。然后只需遍历组并替换 values。
const transform = (json, config) => {
Object.keys(json.groups).forEach(group => {
// Skip groups that are not in config
if (!(group in config)) return;
const keys = Object.keys(json.groups[group]);
for (const key of keys) {
json.groups[group][key] = { ...config[group] };
}
});
};
// All the the groups we want to transform
const config = {
"1": {
"schemas": "block"
},
"101": {
"schemas": "all",
"native": "write"
}
};
const json = {
"revision": 76,
"groups": {
"1": {
"26": {
"schemas": "block"
},
"138": {
"schemas": "block"
},
"179": {
"schemas": "block"
},
"245": {
"schemas": "all",
"native": "write"
}
},
"101": {
"167": {
"schemas": "block"
}
}
}
};
transform(json, config);
console.log(json);
回答3
因为 groups.1
对象应该包含 "schemas": "block"
而 groups.101
对象应该包含 "schemas": "all", "native": "write"
是一个直接的要求。因此,我们可以这样尝试:
const jsonObj = {
"revision": 76,
"groups": {
"1": {
"26": {
"schemas": "block"
},
"138": {
"schemas": "block"
},
"179": {
"schemas": "block"
},
"245": {
"schemas": "all",
"native": "write"
}
},
"101": {
"167": {
"schemas": "block"
}
}
}
};
function transformObj(group1, group101) {
Object.keys(group1).forEach(key => {
group1[key] = {
"schemas": "block"
}
});
Object.keys(group101).forEach(key => {
group101[key] = {
"schemas": "all",
"native": "write"
}
});
}
transformObj(jsonObj['groups']['1'], jsonObj['groups']['101']);
console.log(jsonObj);