Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

266
Visualizações
Filter array of objects and remove object is filter returns empty values

Here is my question. I have an array of objects as shown below. Each object has a title and rows in it. rows is also array of objects with risk values like P1, P2, P3 etc. I want to filter and get only those rows whose risk is equal to P1. In some objects, there might be no risk with P1 at all. In that case, i want to ignore that object completely.

let data = [ 
    {
        "title": "QA",
        "rows": [
            {
                "risk": "P3",
                "Title": "Permission issue",
            }
        ]
    }, 
    {
        "title": "Prod",
        "rows": [
            {
                "risk": "P5",
                "Title": "Console log errors fix",
            },
            {
                "risk": "P1",
                "Title": "Server is in hung state",
            }
        ]
    }
]

I want the result as follows. As you can see the title QA didnt have P1 at all. so i want to ignore that object as a whole.

    {
        "title": "Prod",
        "rows": [
            {
                "risk": "P1",
                "Title": "Server is in hung state",
            }
        ]
    }
]

I tried using this code. but the problem with this is that the title with QA gets added in the final output even though it doesnot have any P1 risk in it.

let result = input.map((obj) => ({
  ...obj,
  rows: obj.rows.filter((row) => row.risk === "P1"),
}));

Can someone let me know how to filter the objects with P1 and skip the whole object if there are no P1 in it.

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

You can filter out those objects without risk === "P1" by using the function Array.prototype.filter.

const data = [     {        "title": "QA",        "rows": [            {                "risk": "P3",                "Title": "Permission issue",            }        ]    },     {        "title": "Prod",        "rows": [            {                "risk": "P5",                "Title": "Console log errors fix",            },            {                "risk": "P1",                "Title": "Server is in hung state",            }        ]    }],
      result = structuredClone(data).filter(o => {
        o.rows = o.rows.filter(({risk}) => risk === "P1");
        return !!o.rows.length;
      });
      
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

about 4 years ago · Juan Pablo Isaza Relatório

0

I would skip the map entirely and only use a filter:

const result = data.filter((obj) => {
  let includesP1 = false;
  obj.rows.forEach((r) => {
    if (r.risk === "P1") includesP1 = true;
  });
  return includesP1;
});
about 4 years ago · Juan Pablo Isaza Relatório

0

You can build up a new list based on those which have rows with P1 risk.

data.reduce((acc, item) => {
  const rows = item.rows.filter(it => it.risk === 'P1');
  if (rows.length === 0) return acc;
  return [...acc, {...item, rows }]
}, [])
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda