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

93
Views
Adding an Array of Objects with some new properties to another Array of Objects which has same name

I have an array of Objects called "value" in my redux store which I'm newly creating by adding start time/end time to it every time the process starts & stops. looks like this

const value = [
{startTime:1635926165, name:"dailyScripts", endTime:1635926189},
{startTime:1635926125, name:"datawarehouse", endTime:1635926125},
{startTime:1234567890, name:"dummp", endTime:1234567890}
]

I have another array of objects in a "local state" called list which has all the data. Here I want to add the start time and end time to each obj that has the same name as above.

const list = [
{pid: 248, name: 'dailyScripts', pm2_env: {…}, pm_id: 2, monit: {…}}
{pid: 259, name: 'datawarehouse', pm2_env: {…}, pm_id: 2, monit: {…}}
{pid: 0, name: 'dump', pm2_env: {…}, pm_id: 2, monit: {…}}
]

so my final output should look like this -

const list = [
{pid: 248, name: 'dailyScripts',...otherObjs, startTime:1635926165, endTime:1635926189}
{pid: 259, name: 'datawarehouse',...otherObjs, startTime:1635926125, endTime:1635926125}
{pid: 0, name: 'dump',...otherObjs, startTime:1635926165, endTime:1635926189}
]

How do I go about it with React? I want to mutate the list state or merge the value arr with the list arr.

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

0

Try this :

    const value = [
      { startTime: 1635926165, name: "dailyScripts", endTime: 1635926189 },
      { startTime: 1635926125, name: "datawarehouse", endTime: 1635926125 },
      { startTime: 1234567890, name: "dummp", endTime: 1234567890 }
    ];
    const list = [
      { pid: 248, name: 'dailyScripts', pm2_env: {}, pm_id: 2, monit: {} },
      { pid: 259, name: 'datawarehouse', pm2_env: {}, pm_id: 2, monit: {} },
      { pid: 0, name: 'dump', pm2_env: {}, pm_id: 2, monit: {} }
    ];
    let output = [];
    list.forEach(e => {
      let valueTime = value.find(ee => ee.name === e.name);
      if (valueTime) {
        output.push({...e, startTime : valueTime.startTime, endTime : valueTime.endTime });
      } else {
        output.push({...e});
      }
    });
    console.log(output);

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!