score:2

Accepted answer

D3 data() method accepts only 3 things:

  • An array
  • A function
  • Nothing

Therefore, you cannot use that object (B) you have. You need to convert it in an array.

For instance, here is a solution (out of many) for converting it into an array similar to your first one (A):

const B = {
  "3": {
    "s1": 0.3,
    "s2": 0.5
  },
  "6": {
    "s1": 0.3,
    "s2": 0.5
  },
  "23": {
    "s1": 0.3,
    "s2": 0.5
  }
};

const data = Object.keys(B).map(function(d) {
  return Object.assign({}, B[d], {
    id: d
  })
});

console.log(data)

The difference here is that id is a string, not a number. If you want a number instead, just coerce it.


Related Query

More Query from same tag