const array = [
{
aQueue: {
active: [
{
name: 'A queue stuff',
data: {
meta: {
consumer: {
id: 1
}
}
}
},
{
name: 'A queue stuff',
data: {
meta: {
consumer: {
id: 2
}
}
}
},
{
name: 'A queue other stuff',
data: {
meta: {
consumer: {
id: 1
}
}
}
}
],
completed: [
{
name: 'A queue other stuff',
data: {
meta: {
consumer: {
id: 2
}
}
}
}
]
}
},
{
bQueue: {
active: [
{
name: 'B queue stuff',
data: {
meta: {
consumer: {
id: 2
}
}
}
}
],
completed: [
{
name: 'B queue other stuff',
data: {
meta: {
consumer: {
id: 1
}
}
}
}
]
}
}
]
id: 1
с сохранением всей структуры, т.е. получить это:const array = [
{
aQueue: {
active: [
{
name: 'A queue stuff',
data: {
meta: {
consumer: {
id: 1
}
}
}
},
{
name: 'A queue other stuff',
data: {
meta: {
consumer: {
id: 1
}
}
}
}
],
completed: []
}
},
{
bQueue: {
active: [],
completed: [
{
name: 'B queue other stuff',
data: {
meta: {
consumer: {
id: 1
}
}
}
}
]
}
}
]
const result = Object.entries(obj).reduce((acc, [key, {active, completed}]) => {
const filter = ({data: {meta: {consumer: {id}}}}) => id === 1;
acc[key] = {
active: active.filter(filter),
completed: completed.filter(filter)
};
return acc;
}, {});
{
aQueue: ...,
bQueue: ...
}