Solved

Javascript d3, get first row only

Posted on 2014-12-18
8
346 Views
Last Modified: 2014-12-18
Hello,

I have the following code and I would like to get just the titles not the other values.

Can you please let me know how to edit this to work OR if there is a better way.

Please note that the csv delimiter is different in a couple of my files.

<script src="http://d3js.org/d3.v3.min.js" charset="utf-8"></script>
<script type="text/javascript">
/*
Example cars.csv:
Year;Make;Model;Length
1997,Ford,E350,2.34
2000,Mercury,Cougar,2.38
*/
d3.csv('cars.csv', function(csv) {
	csv.forEach(function(row) {
		document.write(Object.keys(row));
	});
});
</script>

Open in new window


Thank you!
Matt
0
Comment
Question by:movieprodw
  • 4
  • 4
8 Comments
 
LVL 58

Expert Comment

by:Gary
ID: 40508389
What do you mean the titles - Year;Make;Model;Length ???
The code works but the titles should be seperated by commas.
0
 
LVL 1

Author Comment

by:movieprodw
ID: 40508409
Sorry, the column names
0
 
LVL 58

Expert Comment

by:Gary
ID: 40508415
Then what is wrong with what you have?
If you mean you have semicolons then use
https://github.com/mbostock/dsv
0
 
LVL 1

Author Comment

by:movieprodw
ID: 40508424
Well I JUST want the first row with the names, I do not want it to repeat for every row, I am good at php but not javascript.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 58

Expert Comment

by:Gary
ID: 40508439
<script src="http://d3js.org/d3.v3.min.js" charset="utf-8"></script>
<script type="text/javascript">
d3.csv('cars.csv', function(data){
	d3.keys(data[0]).forEach(function(item){
		console.log(item)
	})
});
</script>

Open in new window

0
 
LVL 1

Author Comment

by:movieprodw
ID: 40508448
Hello Gary,

That worked.

I am confused because the page never stops loading, any ideas?

<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>RSS Feed Manager</title>
<script src="http://d3js.org/d3.v3.min.js" charset="utf-8"></script>
</head>

<body>

<script type="text/javascript">
d3.csv('cars.csv', function(data){
	d3.keys(data[0]).forEach(function(item){
		document.write(item);
	})
});
</script> 

</body>
</html>

Open in new window

0
 
LVL 58

Accepted Solution

by:
Gary earned 500 total points
ID: 40508454
d3.csv('cars.csv', function(data){
      d3.keys(data[0]).forEach(function(item){
            document.write(item);
      })
     document.close()
});
0
 
LVL 1

Author Closing Comment

by:movieprodw
ID: 40508561
You are the man!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Got endorsements from your clients?  Great!  There is almost nothing better than word-of-mouth advertising.  But how can you do that on the internet?  Sure you can make a page for endorsement quotations and list them all, but who is …
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now