Hi @Martien and thanks for the help! And you’re never late to the party!
My specific use case is this: A client and I have agreed to a set of actions (original list). After some time, I am reviewing the status of completed actions. I wish to call in the original list, and then to that list I would like to append my review. Here’s an example:
Original task list:
ID |
Task |
Target Completion Date |
Responsible Party |
1 |
Select wall materials |
30-Aug-21 |
Anna |
2 |
Select wall colors |
10-Aug-21 |
Anna |
3 |
Buy wall materials |
15-Aug-21 |
Aaron |
4 |
Buy wall paints |
15-Aug-21 |
Aaron |
Status review:
ID |
Status |
Revised Competion Date |
Notes |
1 |
Complete |
n/a |
|
2 |
Complete |
n/a |
|
3 |
Complete |
n/a |
|
4 |
Pending |
20-Aug-21 |
Paint wasn’t available. |
The original list informs some other bits of information, so I don’t wish to mutate it. Rather, I wish to reference it, append to it my review, and return a ‘full’ recounting of the situation as an update. I.e. I would like to return this:
ID |
Task |
Target Completion Date |
Responsible Party |
Status |
Revised Competion Date |
Notes |
1 |
Select wall materials |
30-Aug-21 |
Anna |
Complete |
n/a |
|
2 |
Select wall colors |
10-Aug-21 |
Anna |
Complete |
n/a |
|
3 |
Buy wall materials |
15-Aug-21 |
Aaron |
Complete |
n/a |
|
4 |
Buy wall paints |
15-Aug-21 |
Aaron |
Pending |
20-Aug-21 |
Paint wasn’t available. |
To respond to your questions:
For one, what exactly are you trying to achieve?
- the proper use of
Object.assign()
?
Yes, I always would like to better understand the proper use of tools. Generally, I read through the Internet trying to figure out what I am doing and how it can be accomplished with JavaScript. Learning how to ask questions and what to look for remains an ongoing struggle.
- merging two objects into a single one?
Yes, this is absolutely what I am after. From reading I assumed I was on the right path with Object.assign()
- zipping together two equal length arrays of objects into a single list of merged objects?
Yes.
- something else (if so, what exactly)?
Nope, should be covered by the 3 points above!
To what extent does your goal relate to what they call in Functional Programming a pure function:
I have no idea what a ‘pure’ function even means! Just the other day, @chrispahm introduced me to the concept of a function! (I have no computer science background at all…)
- has input parameters;
I don’t know that i really understand what ‘parameter’ means
- does not use or set stateful values (outside of itself);
I don’t know what a ‘stateful value’ is
- return based on input; always returns the same results when given the same input; and
Yes! Always I would like to return the same results when given the same inputs
- has no side effects outside itself (like writing to screen, database, file, network or output channel).
I am not sure? I do intend to render the object into an HTML table, but for now I am just looking to create the object.
This avoids the anti-pattern Mike warns for: Object.assign()
’s primary goal is to create the side effect of changing existing objects (target
in your case).
Your further elaboration :
Object.assign()
modifies target
in place: side effect that can reduce your code’s evolvability (legibility, understandability, maintainability, changeability) and can create all kinds of erratic behavior .
Oh no! Then I don’t want Object.assign()
after all!
Also, do you really need a deep copy of the object(s)?
I don’t wish to mutate the source objects at all. I just want to produce an output that merges the original and the added ‘review’.
My experience is that I never needed a deep copy when I use the pure approach. Simply create new objects from existing ones. The ‘garbage’ this leaves behind will be collected by the system automatically when needed.
One man’s trash…
So, I’d simply do ({...target, ...source})
to merge the two objects.
Cool! Can you elaborate on how this is achieved?
Also see Object.assign()’s ‘impurity’ / Martien van Steenbergen / Observable
Awesome! Thanks!
Just my €.02.
Thanks! In USA dollars, I think that’s almost 3 cents!