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

186
Vistas
eliminar parte de un objeto y colocarlo en otro objeto

Tengo un objeto (valores). Quiero seleccionar el campo que comienza con ('fld_str_) y que contiene un objeto (en este caso, FLD_STR_102). Quiero eliminar todo el objeto de la matriz createValue y colocarlo en un objeto CreateFileValue. Puedes comprobar la salida

Ejemplo de entrada:

 const values = { ID: 748817, createValues: [{ field: "FLD_STR_101", value: 'hello', }, { field: "FLD_STR_102", fileName: "doc.pdf", value: { field: 'FLD_STR_102', fileName: 'bulletin_paie_avril.pdf', value: 'jkhkjhkhkjh' }, } ] }

Salida de ejemplo:

 const values = { ID: 748817, createValues: [{ field: "FLD_STR_101", value: 'hello', } ], createFileValues: { "field": "FLD_STR_102", "fileName": "bulletin_paie_avril.pdf", "value": "jkhkjhkhkjh" } } 

 const values = { ID: 748817, createValues: [{ field: "FLD_STR_101", value: 'hello', }, { field: "FLD_STR_102", fileName: "doc.pdf", value: { field: 'FLD_STR_102', fileName: 'bulletin_paie_avril.pdf', value: 'jkhkjhkhkjh' }, } ] } const res = () => { if (values.createValues.startsWith('FLD_STR_') && typeof values.createValues.startsWith('FLD_STR_') === 'object') { return { }; } } }; console.log(res());

about 4 years ago · Santiago Gelvez
3 Respuestas
Responde la pregunta

0

values.createValues.startsWith('FLD_STR_') no es válido porque createValues es una matriz. Deberá iterar sobre values.createValues para verificar cada field.startsWith('FLD_STR_') y si el valor es un objeto (consulte Cómo detectar si una variable es un objeto de JavaScript puro para ver un ejemplo de esto).

 const values = { ID: 748817, createValues: [{ field: "FLD_STR_101", value: 'hello', }, { field: "FLD_STR_102", fileName: "doc.pdf", value: { field: 'FLD_STR_102', fileName: 'bulletin_paie_avril.pdf', value: 'jkhkjhkhkjh' } } ] } const res = () => { var newResult = { ID: values.ID, createValues: [] }; values.createValues.forEach(function(item) { var newCreateValue = {}; if (item.field.startsWith('FLD_STR_') && item.value.constructor.name === "Object") { newCreateValue.field = item.value.field; newCreateValue.value = item.value.value; newCreateValue.fileName = item.value.fileName; // if this is a createFilesValue, add it as a new key/value to the newResult object newResult.createFileValues = newCreateValue; } else { newCreateValue.field = item.field; newCreateValue.value = item.value; // if this is an existing createValues, add it to the createValues array newResult.createValues.push(newCreateValue); } }); return newResult; }; console.log(res());

about 4 years ago · Santiago Gelvez Denunciar

0

 const values = { ID: 748817, createValues: [{ field: "FLD_STR_101", value: 'hello', }, { field: "FLD_STR_102", fileName: "doc.pdf", value: { field: 'FLD_STR_102', fileName: 'bulletin_paie_avril.pdf', value: 'jkhkjhkhkjh' }, } ] } // build createFileValues structure values.createFileValues = values.createValues.filter(v => v.field.startsWith("FLD_STR_") && typeof v.value === "object"); // filter the files out of createValues values.createValues = values.createValues.filter(v => !values.createFileValues.includes(v)); console.log(values);

about 4 years ago · Santiago Gelvez Denunciar

0

Esto debería funcionar

 const values = { ID: 748817, createValues: [{ field: "FLD_STR_101", value: 'hello', }, { field: "FLD_STR_102", fileName: "doc.pdf", value: { field: 'FLD_STR_102', fileName: 'bulletin_paie_avril.pdf', value: 'jkhkjhkhkjh' }, } ] } const res = (data) => { let oldCreateValues = data.createValues; let oldCreateFileValues = data.createFileValues; let newCreateValues = []; let newCreateFileValues = oldCreateFileValues && Array.isArray(oldCreateFileValues) ? oldCreateFileValues : []; if (oldCreateValues && Array.isArray(oldCreateValues)) { oldCreateValues.forEach(item => { if (item.field.startsWith('FLD_STR_') && item.value && typeof item.value === 'object') { newCreateFileValues.push(item); } else { newCreateValues.push(item); } }); } data.createValues = newCreateValues; data.createFileValues = newCreateFileValues; return data; }; console.log(res(values));
about 4 years ago · Santiago Gelvez 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