Company logo
  • Jobs
  • Bootcamp
  • About Us
  • For professionals
    • Home
    • Jobs
    • Courses
    • Questions
    • Teachers
    • Bootcamp
  • For business
    • Home
    • Our process
    • Plans
    • Assessments
    • Payroll
    • Blog
    • Calculator

0

67
Views
Find an element and add character to the string

I have an object (obj) containing an array (create), I would like to access the create.field and modify 'OWNER' to 'OWNER_ID'. I have other data in the create array so I do not wish to erase it.

 const obj = {
 pro: 0,
 gr: 0,
 create: [
{
  field: "OWNER",
  value: 12345
}
]
};

this is what I have now :

const owner = obj.create.find((ele) => ele.field === 'OWNER'.join('_ID');
console.log('owner', owner);
7 months ago · Juan Pablo Isaza
3 answers
Answer question

0

if I understand correctly you can do this

const newObject = {
    ...obj,
     create: obj.create.map(({ field, value }) => ({ field: field === "OWNER" ? `${field}_ID` : field, value })),
}
7 months ago · Juan Pablo Isaza Report

0

You need to find element first, then you need to assign the owner.field property to its value plus the _ID

 const obj = {
 pro: 0,
 gr: 0,
 create: [
{
  field: "OWNER",
  value: 12345
}
]
};

const owner = obj.create.find((ele) => ele.field === 'OWNER');
owner.field = owner.field +'_ID'
console.log('owner', owner);
console.log('obj', obj);

7 months ago · Juan Pablo Isaza Report

0

Try:

const obj = {
   pro: 0,
   gr: 0,
   create: [
      {
        field: "OWNER",
        value: 12345
      }
   ]
};

let owner = obj.create.find(x => x.field == "OWNER");
if(owner) {
  owner.field = "OWNER_ID";
}
console.log(obj);
7 months 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 job Plans Our process Sales
Legal
Terms and conditions Privacy policy
© 2023 PeakU Inc. All Rights Reserved.