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

96
Visualizações
Remove all array of objects that contain same key and has at least one value true

Hi I have an array of objects which string starts with a specific prefix and if that key value is true then remove all the objects in an array that contains the same key(prefix)

below is the array of object:

const data = [{
    field_name_key: "Recive_IsViaEmail",
    fieldValue: false
  },
  {
    field_name_key: "Recive_IsViaSMS",
    fieldValue: false
  },
  {
    field_name_key: "Sender_IsViaEmail",
    fieldValue: false
  },
  {
    field_name_key: "Sender_IsViaSMS",
    fieldValue: true
  },

]

here "Sender_IsViaSMS" contains true hence remove all the objects that start with the prefix key Sender_IsVia

Final result is this:

const data = [{
    field_name_key: "Recive_IsViaEmail",
    fieldValue: false
  },
  {
    field_name_key: "Recive_IsViaSMS",
    fieldValue: false
  }
]

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

0

An inefficient but short solution would be to use Array.filter and set the condition of the callback to whether data contains an item with the same field_name_key property prefix and whose fieldValue property is true:

const data=[{field_name_key:"Recive_IsViaEmail",fieldValue:false},{field_name_key:"Recive_IsViaSMS",fieldValue:false},{field_name_key:"Sender_IsViaEmail",fieldValue:false},{field_name_key:"Sender_IsViaSMS",fieldValue:true}];

const res = data.filter(e => !data.find(f => f.field_name_key.split("IsVia")[0] == e.field_name_key.split("IsVia")[0] && f.fieldValue))

console.log(res)

about 4 years ago · Juan Pablo Isaza Relatório

0

You can create a function that first finds the entries to remove, then remove one by one:

const data = [{
    field_name_key: "Recive_IsViaEmail",
    fieldValue: false
  },
  {
    field_name_key: "Recive_IsViaSMS",
    fieldValue: false
  },
  {
    field_name_key: "Sender_IsViaEmail",
    fieldValue: false
  },
  {
    field_name_key: "Sender_IsViaSMS",
    fieldValue: true
  },

]

const sanitizeData = (list) => {
    // find entry keys to remove
    const toRemove = list.reduce((prev, el) => {
        if(el.fieldValue === true) return el.field_name_key.split('_')[0]
    })
    // remove
    const removed = list.filter(el => {
        return !toRemove.includes(el.field_name_key.split('_')[0])
    })
    return removed
}

console.log(sanitizeData(data))

about 4 years ago · Juan Pablo Isaza Relatório

0

Build up a list of the prefixes to remove first, then just run a filter.

I am not sure on how you are coming up with the prefix of a given field name however. Given your example you're expecting "Sender_IsViaSMS" to also purge "Recive_IsViaSMS". But you should be able to take this and adapt it as you see fit.

const data=[{field_name_key:"Recive_IsViaEmail",fieldValue:false},{field_name_key:"Recive_IsViaSMS",fieldValue:false},{field_name_key:"Sender_IsViaEmail",fieldValue:false},{field_name_key:"Sender_IsViaSMS",fieldValue:true}];

const prefixsToAxe = new Set();

// Having trouble with this part in your question, so ran with this, replace as you see fit
const determinePrefix = (field_name_key) => field_name_key.split('IsVia')[0];

for (const entryToAxe of data.filter(x => x.fieldValue === true)) {
  const prefixToAxe = determinePrefix(entryToAxe.field_name_key);
  prefixsToAxe.add(prefixToAxe);
}

const purged = data.filter(entry => {
  const prefixInQuestion = determinePrefix(entry.field_name_key);
  return !prefixsToAxe.has(prefixInQuestion);
})

console.log(purged)

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