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
StartsWith() in an array

Basically I am using the values 'FLD_STR_101' to retrieve my file. I have different field starting with 'FLD_STR_' so I cannot base my if statement on this specific field. What I would like is to to do is map and retrieve the field with FLD_STR...something like this

values[startWith('FLD_STR_')]

so then I will be able to check if the field starts with FLD_STR_ and then I will be able to differentiate the field depending on the type of each field(file, text ...)

Here is what I have as an example so you can understand. It seems that I cannot inject the startsWith() inside the array like this. Any clue on how to achieve this ?

    const test =Object.entries(values['FLD_STR_101']).map((entry, key) =>( {
            test: entry[0],
            test2:key
    }))

about 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

An idea can be

  1. to filter the object key that match your expectation
  2. loop with array.forEach method on these filtered keys
  3. doing your map method on values[filteredKey]

const values = {
  FLD_STR_101: {
    test: 1,
    type: 'type1'
  },
  FLD_STR_102: {
    test2: 1,
    type: 'type1'
  },
  FLD_STR_103_NO_TYPE: {
    test2: 1
  },
  NOTFLD_STR_102: {
    test3: 1
  }
};

let test = [];

Object.keys(values)
  .filter(key =>  key.startsWith('FLD_STR_') && values[key]['type'])
  .forEach(filteredKey => {
    test = [
       ...test, 
       ...Object.entries(values[filteredKey]).map((entry, key) => ({
         test: entry[0],
         test2: key
       }))]
    });

console.log(test);

about 4 years ago · Santiago Trujillo Relatório

0

You were almost there, you can use an "startWidth" method, but not directly, try combining with the array method filter, it's cleaner and readable.

const values = {
  FLD_STR_101: {
    test: 1
  },
  FLD_STR_102: {
    test2: 2
  },
  INVALID_STR_103: {
    test3: 3
  }
};
const startWith = (str, prefix) =>  {
  return str.slice(0, prefix.length) === prefix;
}

const test = Object.entries(values)
  .filter(([key]) => startWith(key, 'FLD_STR_'))
  .map((entry, key) =>( {
    test: entry[0],
    test2:key
  }))
about 4 years ago · Santiago Trujillo 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