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

187
Visualizações
Filter array based on check array

Need to filter the arr based on check values. I am unable to filter inside the getItems function.

let check =  ["DIS_1","DIS_2","DIS_6","DIS_10"]
let arr = [{otherProperty: "",cr: [{cc: "DIS_1", cv: "cal1"},{cc: "first", cv: "cal2"},{cc: "DIS_2", cv: "cal3"}]},
          {otherProperty: "",cr: [{cc: "DIS_6", cv: "cal4"},{cc: "second", cv: "cal5"},{cc: "DIS_10", cv: "cal6"}]},
          {otherProperty: "",cr: [{cc: "third", cv: "cal7"},{cc: "fourth", cv: "cal8"},{cc: "DIS_1", cv: "cal9"}]}
          ]

let expectedOutput = [{otherProperty: "",cr: [{cc: "DIS_1", cv: "cal1"},{cc: "DIS_2", cv: "cal3"}]},
          {otherProperty: "",cr: [{cc: "DIS_6", cv: "cal4"},{cc: "DIS_10", cv: "cal6"}]},
          {otherProperty: "",cr: [{cc: "DIS_1", cv: "cal9"}]}
          ]

function getItems(arr) {
  console.log('--arr', arr)
}

console.log('expect', getItems(arr))

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

0

You can do it with one liner:

const getItems = (arr) => arr.map(({cr, ...other_properties})=> ({ ...other_properties, cr: cr.filter(item=>check.includes(item.cc))}));

Here is the working example:

let check =  ["DIS_1","DIS_2","DIS_6","DIS_10"]
let arr = [
  {otherProperty: "",cr: [{cc: "DIS_1", cv: "cal1"},{cc: "first", cv: "cal2"},{cc: "DIS_2", cv: "cal3"}]},
  {otherProperty: "",cr: [{cc: "DIS_6", cv: "cal4"},{cc: "second", cv: "cal5"},{cc: "DIS_10", cv: "cal6"}]},
  {otherProperty: "",cr: [{cc: "third", cv: "cal7"},{cc: "fourth", cv: "cal8"},{cc: "DIS_1", cv: "cal9"}]}
]
          
const getItems = (arr) => arr.map(({cr, ...other_properties})=> ({ ...other_properties, cr: cr.filter(item=>check.includes(item.cc))}));

console.log(getItems(arr));

about 4 years ago · Juan Pablo Isaza Relatório

0

// let check =  ["DIS_1","DIS_2","DIS_6","DIS_10"];
let type = { DIS_1: "first", DIS_2: "two", DIS_6: "six", DIS_10: "ten" };
let arr = [{otherProperty: "",cr: [{cc: "DIS_1", cv: "cal1"},{cc: "first", cv: "cal2"},{cc: "DIS_2", cv: "cal3"}]},
          {otherProperty: "",cr: [{cc: "DIS_6", cv: "cal4"},{cc: "second", cv: "cal5"},{cc: "DIS_10", cv: "cal6"}]},
          {otherProperty: "",cr: [{cc: "third", cv: "cal7"},{cc: "fourth", cv: "cal8"},{cc: "DIS_1", cv: "cal9"}]}
          ]

// using array
// const res = arr.map(item => ({...item, cr : item.cr.filter(o => check.includes(o.cc))}));

// using object
const res = arr.map(item => ({...item, cr : item.cr.filter(o => Object.keys(type).includes(o.cc))}));

console.log(res);
.as-console-wrapper { max-height: 100% !important; top: 0 }

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