Básicamente estoy usando los valores 'FLD_STR_101' para recuperar mi archivo. Tengo un campo diferente que comienza con 'FLD_STR_', por lo que no puedo basar mi declaración if en este campo específico. Lo que me gustaría hacer es mapear y recuperar el campo con FLD_STR... algo como esto
values[startWith('FLD_STR_')]entonces podré verificar si el campo comienza con FLD_STR_ y luego podré diferenciar el campo según el tipo de cada campo (archivo, texto ...)
Esto es lo que tengo como ejemplo para que puedas entender. Parece que no puedo inyectar el startWith() dentro de la matriz de esta manera. ¿Alguna pista sobre cómo lograr esto?
const test =Object.entries(values['FLD_STR_101']).map((entry, key) =>( { test: entry[0], test2:key }))Una idea puede ser
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);Ya casi había llegado, puede usar un método "startWidth", pero no directamente, intente combinarlo con el filtro del método de matriz, es más limpio y legible.
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 }))