javascript - 如何拼接第 0 个索引下的每个“条件”特定键“逻辑”和嵌套 JSON 的值?
问题描述
我有一个嵌套对象,我想在每个“条件”中找到拼接:第 0 个索引键“逻辑”和其中的值。假设对象看起来像这样:
原始输入:
[
{
"conditions": [
{
"logic": "AND",
"parameter": "Risk Engine Score",
"condition": "Equals",
"value": "122",
"level": "first",
"type": "condition"
},
{
"level": "second",
"type": "group",
"nextChildLogic": "AND",
"conditions": [
{
"logic": "AND",
"parameter": "Risk Engine Score",
"condition": "Equals",
"value": "123",
"level": "second",
"type": "condition"
},
{
"logic": "AND",
"parameter": "Risk Engine Score",
"condition": "Equals",
"value": "35645",
"level": "second",
"type": "condition"
}
],
"groupLogic": "AND"
}
],
"modeOfAuth": "otp"
},
{
"conditions": [
{
"logic": "AND",
"parameter": "Risk Engine Score",
"condition": "< Less than",
"value": "12",
"level": "first",
"type": "condition"
}
],
"modeOfAuth": "frictionless"
},
{
"conditions": [
{
"logic": "AND",
"parameter": "Risk Engine Score",
"condition": "Equals",
"value": "12",
"level": "first",
"type": "condition"
},
{
"level": "second",
"type": "group",
"nextChildLogic": "AND",
"conditions": [
{
"logic": "AND",
"parameter": "Amount",
"condition": "< Less than",
"value": "12",
"level": "second",
"type": "condition"
},
{
"logic": "AND",
"parameter": "Risk Engine Score",
"condition": "Equals",
"value": "345",
"level": "second",
"type": "condition"
}
],
"groupLogic": "AND"
}
],
"modeOfAuth": "frictionless"
}
]
预期输出:
[
{
"conditions": [
{
"parameter": "Risk Engine Score",
"condition": "Equals",
"value": "122",
"level": "first",
"type": "condition"
},
{
"level": "second",
"type": "group",
"nextChildLogic": "AND",
"conditions": [
{
"parameter": "Risk Engine Score",
"condition": "Equals",
"value": "123",
"level": "second",
"type": "condition"
},
{
"logic": "AND",
"parameter": "Risk Engine Score",
"condition": "Equals",
"value": "35645",
"level": "second",
"type": "condition"
}
],
"groupLogic": "AND"
}
],
"modeOfAuth": "otp"
},
{
"conditions": [
{
"logic": "AND",
"parameter": "Risk Engine Score",
"condition": "< Less than",
"value": "12",
"level": "first",
"type": "condition"
}
],
"modeOfAuth": "frictionless"
},
{
"conditions": [
{
"logic": "AND",
"parameter": "Risk Engine Score",
"condition": "Equals",
"value": "12",
"level": "first",
"type": "condition"
},
{
"level": "second",
"type": "group",
"nextChildLogic": "AND",
"conditions": [
{
"parameter": "Amount",
"condition": "< Less than",
"value": "12",
"level": "second",
"type": "condition"
},
{
"logic": "AND",
"parameter": "Risk Engine Score",
"condition": "Equals",
"value": "345",
"level": "second",
"type": "condition"
}
],
"groupLogic": "AND"
}
],
"modeOfAuth": "frictionless"
}
]
代码:
const parseData = (data) => data.map(({conditions, ...rest}) => conditions.map(({logic, ...conditions}) => ({conditions, ...rest})));
console.log(parseData(data));
试过上面的代码。所有索引中的上述 JSON 对象仅需要删除第 0 个索引“逻辑”键下的每个条件
解决方案
var recursive = b =>
b.map((val, i) => {
if (i === 0) delete val.logic;
if (val.conditions) {
val.conditions = recursive(val.conditions);
}
return val;
});
像这样的东西应该可以工作,如果它是数组的第一个元素,我只是删除它,并且还为这些对象内logic
的每个嵌套数组递归调用此函数。conditions
推荐阅读
- twitter-bootstrap - 如何以自举形式并排放置两个输入组
- azure - 如何在没有 Livy 的情况下将 Spark 作业远程提交到 Azure HDInsights 集群
- python - 在 ManyToManyField 中定义最大关系
- python-3.7 - 如何将python代码转换为应用程序
- r - 创建一个向量序列并替换一些元素
- oracle - 从 11g 到 19c 的所有 Oracle 服务器的 Oracle 数据库连接
- php - 作曲家:“警告:您应该避免在更改身份验证设置后覆盖已定义的身份验证设置”?
- python - plotnine geom_boxplot 忽略了必需的审美,需要不必要的审美
- html - 使用 wkhtmltopdf 节点模块转换为 pdf 时,表格行被剪切
- sql - 带有爬网数据的 SQL 连接表插入