0

I'm working on d3.js, I am trying to make a stacked circles chart. I met some new stuff, like functions are defined

function myfct (xxx, {parameters}={}){ ... }

I interpreted the ", {}" part as the definition of optional parameters (? thanks for any further explanation on that)

My main question however is

chart = Pack(rollup, {
  value: ([, value]) => value,
  label: ([key]) => key?.split(/\s+/g).join("\n"),
  title: ([key], n) => `${n.depth 
    ? `${n.ancestors().reverse().slice(1).map(({data: [key]}) => key).join("\n")}\n`
    : ""}${n.value.toLocaleString("en")}`,
  width: 2000,
  height: 2000,
})

This is the code they use to call the chart function, I couldn't find someplace to understand the usage of [, value] and [key], so that I can use them for my own case ? If anyone could explain or point me where to look, would be of much help, thanks !

Zee
  • 1
  • That is JavaScript destructuring assignment . Basically they are only interested in the "value" property when they do [,value] https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment – Amit Feb 23 '22 at 20:27
  • 1
    @Amit Not the `value` property. The second element in the array/iterable that is passed as an argument. – Ivar Feb 23 '22 at 20:29
  • Object destructuring and optional parameters is the only thing going on there. Google search both and you'll be good to go. – code Feb 23 '22 at 20:40
  • @ivar... Correct. value will hold the second element of the array – Amit Feb 23 '22 at 20:40
  • You guys are awesome ! – Zee Feb 23 '22 at 20:49

0 Answers0