javascript - How to print out all attributes of an object -
this question has answer here:
i'm using d3 , when hover on node want text box appear nodes attributes written it. have made text box , im able write attributes know text box :
function onhover(){ d3.selectall("#nodeattributes") .text(function() { return (d.type); }) //random attribute know ; }
this called on 'mouseover' on node. if dont know attributes node has ? how can loop through attributes , write of them text box. data looks similar :
nodes: [ { "type": "o", "name": "fred", "age": "16", "class": "maths", . . . . },
what want outputted text :
type: o name: fred age: 16 class: maths
i'm unsure how loop through each attribute of chosen node (d)
thanks in advance
you can iterate in object following code :
function onhover() { d3.selectall('#nodeattributes').html(function (d) { var str = ''; (var key in d) { str += key + ': ' + d[key] + '<br/>' } return str; } ); }
Comments
Post a Comment