« first day (4634 days earlier)      last day (539 days later) » 

JK.
JK.
06:39
@VLAZ-onstrike- that works very nicely thanks... is it possible to do to multiple levels? eg parent -> child -> grandchild? I know flatmap is like a shorthand for .map().flat(1) so I'm not sure if this would be possible? eg
[
{"id": 1, "name": "parent test1", children: [{"child_id": 1, "child_name": "child one"}, {"child_id": 2, "child_name": "child two", grandchildren: [{"grandchild_id": 1, "grandchild_name": "grandchild one"}]}] }
]

// converts to

[
{"id": 1, "name": "parent test1"},  // just the parent object fields
{"id": 1, "name": "parent test1", "child_id": 1, "child_name": "child one"},  // parent fields and child one fields
{"id": 1, "name": "parent test1", "child_id": 2, "child_name": "child two"},  // parent fields and child two fields
I'm thinking that if the call has something like children.flat(1) or grandchildren.flat(1) in the middle that would mean it is already flattened before it gets to the flatMap and it might work that way?
 
3 hours later…
10:01
@JK. if you want to do this as a single chain, it starts becoming unwieldy - you'd have to nest the calls to do it effectively:
data
    .flatMap(parent => [parent].concat(
      parent.children
        .flatMap(child => [child].concat(
          child.grandchildren
            ?.map(grandchild => ({...child, ...grandchild})) ?? []
          )
        )
        .map(child => ({...parent, ...child}))
      )
    )
not pretty.
Instead you can abstract away some functions and use a tiny bit of recursion:
||> const merge = (a, b) =>
  ({...a, ...b});

const mergeOneToMany = (parent, children = []) =>
  [parent].concat(children.map(child => merge(parent, child)));

function traverseAndCombine(childProps, parent) {
 if (childProps.length === 0)
   return parent;

  const childProp = childProps[0];
  if (!parent[childProps[0]])
    return parent;

  const {[childProp]: children, ...parentData} = parent;

  return mergeOneToMany(
    parentData,
    children.flatMap(child => traverseAndCombine(childProps.slice(1), child))
@VLAZ-onstrike- 'SyntaxError: missing ) after argument list' Logged: [ ] Took: 50ms
...great, syntax error.
Wait, what syntax error? This runs: jsbin.com/wimoveqomi/1/edit?js,console
James doesn't like some syntax there, I suppose
 
3 hours later…
12:53
@VLAZ-onstrike- I don't think [childProp]: is valid Javascript lol
@ParkingMaster destructuring a dynamic key off an object. You can see it work on JSBin, it's not like I made it up.
||> const key = "foo"; const { [key]: bar } = { foo: "hello" }; console.log(bar);
@VLAZ-onstrike- undefined Logged: [ '"hello"' ] Took: 0ms
James understands that bit. So it struggles with something different.
13:13
@VLAZ-onstrike- ok lol. I don't see anything wrong with your code, guess James is buggy today
||> const merge = (a, b) =>
  ({...a, ...b});

const mergeOneToMany = (parent, children = []) =>
  [parent].concat(children.map(child => merge(parent, child)));

function traverseAndCombine(childProps, parent) {
 if (childProps.length === 0)
   return parent;

  const childProp = childProps[0];
  if (!parent[childProps[0]])
    return parent;

  const {[childProp]: children, ...parentData} = parent;

  return mergeOneToMany(
    parentData,
    children.flatMap(child => traverseAndCombine(childProps.slice(1), child))
@ParkingMaster 'SyntaxError: missing ) after argument list' Logged: [ ] Took: 0ms
Yep, tried it on codepen too... it's just James, he must be using some old Javascript preprocessor 😂
 
3 hours later…
16:20
@ParkingMaster Didn't know that. - 0 works like magic. The problem I found w/ the .toFixed(...) was it was truncating the number rather than rounding it
Found this function, which of course won't work on scientific numbers:
function round(value, decimals) {
	return Number(Math.round(value + "e" + decimals) + "e-" + decimals);
}
Another post said to use Math.round(x * 1000) / 1000
This way we're not converting to string, rather working with numeric value
I'd like to keep it as numeric and wondering if that last snippet has any drawbacks
This snippet: Math.round(x * 1000) / 1000
17:15
20 messages moved to Trash can
odd
3 messages moved to Trash can
17:33
@Alex Yes, values that are too large may lose accuracy when multiplied by 1000. As can small values
||> Math.round(0.9998 * 1000) / 1000
@VLAZ-onstrike- 1 Logged: [ ] Took: 0ms
||> Math.round(90071992547409 * 1000) / 1000
@VLAZ-onstrike- 90071992547408.98 Logged: [ ] Took: 1ms
 
5 hours later…
22:51
@JBis did you fix James? He's a little buggy lol

« first day (4634 days earlier)      last day (539 days later) »