У меня есть вложенный объект, и я хочу найти соединение для каждого "условия": 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-го индекса необходимо удалить