Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

180
Views
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 answers
Answer question

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 Report

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!