Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

183
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda