1

I have an array of objects like so:

var dataObj = [{stuff:"thing"}, {other:"something"}, {you:"get"}, {the:"idea"}];

And I'm attempting to append one div with the key in it as text per object:

myElement
 .selectAll("div.myClass")
 .data([dataObj])
 .enter()
 .append("div")
 .attr('class', 'myClass')
 .text(function(d, i) { 
    return Object.keys(d)[i];
});

But this creates only one div element inside myElement like so:

<div id='myElement'><div class='myClass'>stuff</div></div>

What am I missing?

I also tried

myElement
 .selectAll("div.myClass")
 .data(dataObj)
 .enter()
 .append("div")
 .attr('class', 'myClass')
 .text(function(d, i) {
    console.log("This never shows"); 
    return Object.keys(d)[0];
});

to no avail.

1 Answer 1

1

dataObj is already an array. So it looks like you don't need to put in array

myElement
.selectAll("div.myClass")
.data(dataObj)
.enter()
.append("div")
.attr('class', 'myClass')
.text(function(d, i) { 
 return Object.keys(d)[i];
});
Sign up to request clarification or add additional context in comments.

3 Comments

I had tried that originally, text callback never even gets called. See updated OP.
jsfiddle.net/freer4/3wqey0kg Yeah made my own lol it does work. But not working in my live code... maybe I missed something.
Oh in my live code the values aren't an array of objects, just one big object of key-value pairs.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.