1. 程式人生 > >Ask HN: What are you currently working on in your spare time?

Ask HN: What are you currently working on in your spare time?

With the spread (...foo) operator you can already merge json objects. However in order to use it you need to have access to the objects you make reference to.

This is how it would work: In the front end (like in react-redux) you have your state variable and then you normally make a request to the backend, which the backend returns this "JSON2" object, which you can merge automatically (with JSON2.merge method) to create the new state, without requiring reducers. The only thing is that the back-end would need to be aware of how the front-end state structure looks like (it doesn't need to keep track of the current state though).

// the front-end state

let state = { some: "things", nested: { something: { what: "what", } }, other: [2,3] }

// action is a string with JSON2 format that comes from a query to the back-end. Take the "..." literally

let action = "{ ..., nested: { something: { ..., more: "manymore" } }, other: [1, ...] }"

// create the new state

state = JSON2.merge(state, action)

// now state is:

state = { some: "things", nested: { something: { what: "what", more: "manymore" } }, other: [1,2,3] }