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

111
Visualizações
Typescript filter to select object with value of specific words

I have an array of objects in my DB records.

The record:

{"ID123":{"FileID":"12345","FileName":"ABCFile_ver5_44453.PDF"},"DocID":6009,"DocFormat":"PDF"}

The format to store the filename in my DB is always with "FileName": "actual_fileName.PDF".

I want to only get the object with "FileName":"....", to only display the filename instead of other objects.

This is my code:

getValue(): Atts[] | any {
    if (this.isEmptyValue()) {
      return [];
    }
    return !this.useObject ? this.value : Object.entries(this.value).map(([key, value]) => ({ key, value })).filter(value=);
  }

How do I filter the object that contains "FileName" so that I can display the filename in my application? I'm stuck at the filter method.

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

0

I had to reduce your code a little to a minimal reproducible example, and I made the assumption that this.value in your Object.entries is the entire DB record that you have listed above. I converted this to an object so I could process it in my code. So, your mileage may vary if my assumption was incorrect.

let obj = {
  "ID123": {
    "FileID": "12345",
    "FileName": "ABCFile_ver5_44453.PDF"
  },
  "DocID": 6009,
  "DocFormat": "PDF"
};

let result = { "FileName": Object.entries(obj).map(([key, value]) => ({
  key,
  value
})).filter(keyValuePair => {
  if (keyValuePair.value.FileName) {
    return true;
  }
})[0].value.FileName };

This returns:

{
  "FileName": "ABCFile_ver5_44453.PDF"
}

Your filter step is filtering an array of key value pairs, so when filtering, you need to return true only if the 'value' is an object that has a property of FileName.

EDIT:

I realized that the way I wrote this will not work if the returned object from the array does not have value (or is undefined), so it's probably a better idea to store this in a variable first, and then return an object based on that varible, like so:


let fileNameObj = Object.entries(obj).map(([key, value]) => ({
  key,
  value
})).filter(keyValuePair => {
  if (keyValuePair && keyValuePair.value.FileName) {
    return true;
  }
})[0];

if (fileNameObj && fileNameObj.FileName) {
  let result = { "FileName": fileNameObj.FileName };
}

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