1

JS's map method transforms datasets then and there; hence its use in React sometimes appears more convoluted than other available methods, like spread.

Object.assign pops up as a nuanced method I've recently encountered, and I'm curious when it's preferred to the spread method, especially among framework applications built upon JS.

I'm inclined to describing Object.assign as verbose: i.e. https://medium.com/@corinnemariekelly/object-assign-vs-spread-operator-577c889dbadc.

Outside of usage for very specific immediate rendering, however, both methods come up for managing dynamic data without overwriting, and across contexts that 1) don't invoke immediate changes to the copied datasets, or 2) must step through multiple data transformations based on multiple inputs, thereby themselves involving map in additional ways.

Are there some best patterns that combine across 2 or all 3 of the named methods?

Dave Newton
  • 156,572
  • 25
  • 250
  • 300
  • 1
    I think it is just a matter of preference. The spread operator is easier to read for me once you get used to it. At the core the test is "does old state" == "new state" so creating a new object/array is pretty critical. There might be some more in depth answers. Look at the transpiled code. `{...}` often becomes `Object.assign(...)` – Daniel B. Chapman Jun 19 '21 at 18:54
  • 1
    But there is a case if you are trying the spread operator in an array and if you have a null value you will be getting an error, but in the object if you are trying to spread it just skips the error – Learner Jun 19 '21 at 19:00
  • 2
    The second part of your question seems to be another question. You should stick to one question. – trincot Jun 20 '21 at 13:04
  • Are you asking two questions, or one? I don't understand the "this" part of the question(s). Re: assign vs spread--they have different semantics, see the "assign" docs and refs like https://stackoverflow.com/q/32925460/438992 – Dave Newton Jun 20 '21 at 13:06
  • Thanks, Learner, an object performing spread won't return an error but rather skip cases of null in dataset. I believe I encountered that, and it definitely sets a priority across input datasets. – Jess Renard Jun 20 '21 at 13:15
  • I don't get it. You use "this" when you need to access something attached to the instance in question. You use an object when you need an object. The first seems obvious, the latter seems context-dependent, hence no general answer. – Dave Newton Jun 20 '21 at 13:20
  • Hi Dave Newton. I have the docs up this morning, that's funny. It seems `this` is more a reflection of choices as to where to use an object. But also among the priority of keeping the programming in functional paradigm. I'm inclined to the `spread` operator, but `assign` seemed to pop up. Dmitri happened to address some history here: https://dmitripavlutin.com/object-rest-spread-properties-javascript/ – Jess Renard Jun 20 '21 at 13:32
  • @JessRenard The use of `this` in React/Redux is no different than any other JS (and really, it *can’t* be). Your linking of `this` with “where to use an object" will likely require some explanation to get any meaningful input. What is “standard value assignment” and how does it relate to deciding if “objects are called for”? I still don’t understand why `map` is in the question(s) as it’s neither asked about nor discussed. – Dave Newton Jun 20 '21 at 13:40
  • @DaveNewton There's such versions of JS. `This` gets complicated with arrow functions, yet React is of an age celebrating arrow functions. @Learner above already mentioned how an object utilizing spread can skip null data input from any dataset, and it explains the strongly oop conventions I'm finding in React, while React also strongly avoids altering datasets. OOP & FP are often treated as separate paradigms, with the prevailing concept being to avoid catastrophic bugs of changes in state. I'm understanding I need not/shall not ever avoid `this` in React, just provide pertinent binding. Yes? – Jess Renard Jun 20 '21 at 13:57
  • 1
    @JessRenard Yes, "this" requires an appropriate binding, React or not, whatever "appropriate" means in context. "assign" vs spread doesn't have much to do with OOP, and the biggest difference is that assign "set"s rather than copies directly; I don't see how skipping null data has anything to do with "strongly oop conventions". Still don't know why "map" is in the question at all. Questions should be focused, explicit, and precise. – Dave Newton Jun 20 '21 at 14:10
  • @DaveNewton Useful patterns. "That." None do enough on their own. Clustering them seems the best way. I appreciate the perspective point on how pertinence is always established, however. Binding assumptions are statedly altered, is the point, and there's a lot of OO coding that the functional paradigm looks to actively cut through. I actually found similar lines of questions about "this" for both React and arrow functions generally, so I trimmed the category question from here. When it comes down to it, manually binding still marks much less overhead to weaving through clunkier objects. – Jess Renard Jun 20 '21 at 15:09
  • Your question would be much clarified if you would add some example code, and pinpoint where you see a problem in using `Object.assign` in combination with `Array.prototype.map` -- which I think your question comes down to. (NB: no idea what "You're scary math" means). – trincot Jun 20 '21 at 15:50
  • Okay @trincot, thank you. Somewhere the stackoverflow forum mentions that questions on programming approaches are appreciated, and I'll dig into more in-app usages. But exactly, I see and am using `spread` with `map` often, yet I am confused about how to catalog `Object.assign` for the same or even specially-suited utility. – Jess Renard Jun 20 '21 at 16:09
  • @DaveNewton The first answers were very direct and managed to give answers to technical features of the inquiry. No worries if React is not your main meal. – Jess Renard Jun 20 '21 at 20:57
  • Noting that the linked Medium article doesn't really go into any actual details, and the red herring of the "MDN docs using the spread operator" is incorrect, the MDN docs use `...` to indicate the function takes an arbitrary number of parameters. If the question boils down to "when should I use `...` vs `assign` I think it's a reasonable question, but should probably be edited down to reflect this. – Dave Newton Jun 21 '21 at 17:52
  • Precisely, regarding the Medium feature's address of any most useful or particular contexts for `assign.` MDN pointedly highlights that `assign` is not a deep clone (anymore than `spread`.) Another post's finale focused on considering that edge cases not break code, a la handling of multidimensional arrays. Sam...Ming's blog crowd sources in some splendid ways. It mentions: in practice, `assign` might spot-correct non-dimensional data due to its merging to overwrite [by setting a new value] to a `const` object key (illegal to `spread` via conventions promoted by the preferred use of `const`.) – Jess Renard Jun 21 '21 at 22:09
  • `Spread` is always used w/ a new `const` if not with `let`. `Assign` is therefore somewhere to look if data bugs out a program from reassignment on a `const`. All this most applies for shallow clones, none multidimensional (whereby deep key value pairs certainly semantically bug among either of the subject methods.) – Jess Renard Jun 21 '21 at 22:09

0 Answers0