Если структура ваших данных будет довольно непротиворечивой (т.е. похожей на ту, что вы включили в свой вопрос), вы можете сделать что-то вроде этого:
const mapObj = (f, obj) => {
return Object.keys(obj).reduce((acc, key) => {
acc[key] = f(obj[key], key)
return acc
}, {})
}
const filterData = data => {
// the data itself is an array, so iterate over each item in the array
return data.map(x1 => {
// x1 is an object, so need to iterate over each item in the object
return mapObj(x2 => {
// x2 is an object, so need to iterate over each item in the object
return mapObj(x3 => {
// x3 is an array of objects. each item in the array has an action key which could equal "NEW" or "OLD". get rido of the items with action === "OLD"
return x3.filter(x4 => x4.action !== "OLD")
}, x2)
}, x1)
})
}
const data = [
{
"clientDetails": {
"personalData": [
{
"action": "NEW",
"id": "12345"
},
{
"action": "OLD",
"id": "12445"
}
]
},
"clientAddress": {
"primaryAddress": [
{
"action": "OLD",
"id": "12345"
},
{
"action": "NEW",
"id": "12445"
}
],
"secondaryAddress": [
{
"action": "NEW",
"id": "12345"
},
{
"action": "OLD",
"id": "12445"
}
]
}
},
{
"clientDemise": {
"deathDetails": [
{
"action": "NEW",
"id": "12345"
},
{
"action": "OLD",
"id": "12445"
}
]
},
"clientMarital": {
"divorceInformation": [
{
"action": "OLD",
"id": "12345"
},
{
"action": "NEW",
"id": "12445"
}
],
"marraigeInformation": [
{
"action": "NEW",
"id": "12345"
},
{
"action": "OLD",
"id": "12445"
}
]
}
}
]
const result = filterData(data)
console.log(result)
Если вы хотите более общее решение, которое может принимать данные любой структуры и просто удаляет все объекты с действием, равным 'OLD':
const reduceObj = (f, initial, obj) => {
return Object.keys(obj).reduce((acc, key) => {
return f(acc, obj[key], key)
}, initial)
}
const isObject = x => x !== null && typeof x === 'object'
const removeAllOld = data => {
if(Array.isArray(data)) {
return data.reduce((acc, value) => {
// don't include the item if it has a key named 'action' that is equal to 'OLD'
if(value.action && value.action === 'OLD') return acc
acc.push(removeAllOld(value))
return acc
}, [])
}
else if(isObject(data)) {
return reduceObj((acc, value, key) => {
// don't include the item if it has a key named 'action' that is equal to 'OLD'
if(value.action && value.action === 'OLD') return acc
acc[key] = removeAllOld(value)
return acc
}, {}, data)
}
else {
return data
}
}
const data = [
{
"clientDetails": {
"personalData": [
{
"action": "NEW",
"id": "12345"
},
{
"action": "OLD",
"id": "12445"
}
]
},
"clientAddress": {
"primaryAddress": [
{
"action": "OLD",
"id": "12345"
},
{
"action": "NEW",
"id": "12445"
}
],
"secondaryAddress": [
{
"action": "NEW",
"id": "12345"
},
{
"action": "OLD",
"id": "12445"
}
]
}
},
{
"clientDemise": {
"deathDetails": [
{
"action": "NEW",
"id": "12345"
},
{
"action": "OLD",
"id": "12445"
}
]
},
"clientMarital": {
"divorceInformation": [
{
"action": "OLD",
"id": "12345"
},
{
"action": "NEW",
"id": "12445"
}
],
"marraigeInformation": [
{
"action": "NEW",
"id": "12345"
},
{
"action": "OLD",
"id": "12445"
}
]
}
}
]
console.log(removeAllOld(data))