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

248
Views
Cómo actualizar los valores clave en nombre de los Id.

Tengo estos objetos de JavaScript

 var oldObject=[{ id:1, key:'name', fiendSize:'6', fromPlaceHolder:'', fieldInstruction:'', filedType:'input', lable:"Single Line ", type:'text', formValidation: { required:false } }]; var newObject= { id:1, key:'name', fiendSize:'12', fromPlaceHolder:'Enter Your Name', fieldInstruction:'please Enter string only', filedType:'input', lable:"Single Line ", type:'text', formValidation: { required:true } };

Necesito reemplazar/actualizar el valor de oldObject con el valor de newObject con la misma identificación.

Así que aquí está el resultado que quiero obtener:

 var oldObject = [{ id:1, key:'name', fiendSize:'12', fromPlaceHolder:'Enter Your Name', fieldInstruction:'please Enter string only', filedType:'input', lable:"Single Line ", type:'text', formValidation: { required:true }]

¿Cómo puedo implementarlo usando JavaScript en react js?

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

Puedes usar Object.entries() como:

 var oldObject = [{ id: 1, key: 'name', fiendSize: '6', fromPlaceHolder: '', fieldInstruction: '', filedType: 'input', lable: "Single Line ", type: 'text', formValidation: { required: false } }]; var newObject = { id: 1, key: 'name', fiendSize: '12', fromPlaceHolder: 'Enter Your Name', fieldInstruction: 'please Enter string only', filedType: 'input', lable: "Single Line ", type: 'text', formValidation: { required: true } }; let result = [{}]; for (const [key, value] of Object.entries(oldObject[0])) { result[0][key] = newObject[key]; } console.log(result);

Referencia:

  • Objeto.entradas()

PD: usé un objeto nuevo, pero puede actualizar el objeto anterior y codifiqué el número de matriz, por lo que si tiene más de 1, debe usar for-loop .

about 4 years ago · Juan Pablo Isaza Report

0

Dado que se trata de React, debería buscar estructuras de datos inmutables .

filter los objetos que no coincidan con la identificación y luego agregue el objeto actualizado.

 const arr = [ { id: 1, name: 1 }, { id: 2, name: 2 }, { id: 3, name: 3 } ]; const newArr = [ ...arr.filter(obj => obj.id !== 2), { id: 2, name: 'Billy Joel' } ]; console.log(newArr);

Documentación adicional

  • Difundir sintaxis
about 4 years ago · Juan Pablo Isaza Report

0

Usando Array.map y fusionar 2 objetos por {...existItem, ...newObject} .

 var oldObject = [{ id: 1, key: 'name', fiendSize: '6', fromPlaceHolder: '', fieldInstruction: '', filedType: 'input', lable: "Single Line ", type: 'text', formValidation: { required: false } }]; var newObject = { id: 1, key: 'name', fiendSize: '12', fromPlaceHolder: 'Enter Your Name', fieldInstruction: 'please Enter string only', filedType: 'input', lable: "Single Line ", type: 'text', formValidation: { required: true } }; var result = oldObject.map(item => { var existItem = oldObject.find(e => e.id == newObject.id); if (existItem) { return {...existItem, ...newObject}; } return item; }); console.log(result);

about 4 years ago · Juan Pablo Isaza 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!