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

250
Views
How to update key values behalf of Id's

I have these JavaScript objects

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
            }
        };

I need to replace/update the oldObject value with newObject value from with the same id.

So here is the result I want to get:

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
            }]

How can I implement it using JavaScript in react js ?

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

0

You can use Object.entries() like:

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);

Reference:

  • Object.entries()

P.S. i used new object but you can update the old object and i hardcoded the array number so if you have more then 1 you need to use for-loop.

about 4 years ago · Juan Pablo Isaza Report

0

Since this is React you should be looking towards immutable data structures.

filter out the objects that don't match the id, and then add in the updated object.

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);

Additional documentation

  • Spread syntax
about 4 years ago · Juan Pablo Isaza Report

0

Using Array.map and merge 2 object by {...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!