How to create an object from two existing objects that are "similar"

I was given these two arrays of objects:1-

{
completed_on: "08/24/2021",
contractor: null, 
created_on: "08/24/2021",
description: "Testing",
documents: [],
due_date: "08/05/2021",
equipment: 86877,
id: 1493,
name: "Testing for Demo", urgent: false
}

2-

{
action: "Complete",
contractor: null,
created_on: "08/26/2021",
date: "08/26/2021",
equipment:  { label: "Mercruiser | 8.2 HO | 2A453486"},
hours: 0,
id: 56185,
invoice: null,
note: "Closed one time",
schedule: "One time task to close",
task: {
 created_on: "2018-04-04",
 description: "It is advisable to replace sensors every three years due to harsh environmental conditions encountered in marine applications.",
 due_date: "2018-11-26",
 due_hours: null,
 equipment_id: 66036,
 name: "Replace Sensors",
  schedule_id: 4101,
  task_id: 56185,
  type: 1 updated_on: "2018-04-04"
 }
}

And I would like to get an object like the one below. After processing both objects end up populating a new objects with these fields generated dynamically and conditionally depending on the data of the structure and data of the objects above.

{
taskStatus: String,
taskNote: String,
due_date: String,
taskTitle: String,
isRecurringTask: Boolean,
isOneTimeTask: Boolean,
isCorrectiveTask: false,
taskDescription: String,
taskId: Number,
taskHours: Number,
taskPrevNextDue: String,
taskCompletedOn: String,
taskCreatedOn: String,
taskEquipmentId: Number
}

I saw that Array,prototype.reduce method might do the job but not sure how to use it since most documentation is pretty extensive explaining some other kind of scenarios.

Hopefully I’m explaining myself good enough so you guys can better understand to better help me,

Thanks in advance!

I was given these two arrays of objects:

I don’t see any arrays of objects. I see two objects. Are you saying that they will be arrays of objects like those?

So, are you saying that element 0 of the first array will be combined with element 0 of the second array to produce element 0 in the final array?

I saw that Array,prototype.reduce method might do the job but not sure how to use it since most documentation is pretty extensive explaining some other kind of scenarios.

Since there are two arrays, I doubt it. To be honest, as much as I prefer the prototype methods, if you are really merging these two arrays, merging the corresponding elements, then I don’t think any of the prototype methods would be semantically appropriate. If I’m understanding your needs, then I think a simple for loop and pushing the new element onto the final array would be the most clear.

2 Likes

If I had to guess, I would say it looks like you have an array of job objects, and an array of task objects. You are trying to associate the task objects to their related job.

The way I approach this kind of question is to look at one job. That one job may relate to multiple tasks, what can I build one complex job objects from that simple job object and related tasks array?

This action is considered a reducer, of sorts, but not the array reducer - you are not dealing with an array, but an object.

Instead you need to find a way to take one main object and within that, reference any related job objects in a separate array, and merge them or reduce them into the original main object. This is a reducer in the sense of react or redux, not in the sense of an array. These are similar actions, but there is no object.reduce method - we need to create our own.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.