Solved

Javascript d3, get first row only

Posted on 2014-12-18
8
368 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
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
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
 
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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Add Clickable Links to Google Map Markers 3 28
age from date of birth 4 41
Get the number of current month days 2 23
HTML5 frame 5 25
jQuery is a JavaScript library that greatly simplifies JavaScript programming. AJAX is an acronym formed from "Asynchronous JavaScript and XML."  AJAX refers to any communication between client and server, when the human client does not observe a…
This article discusses how to create an extensible mechanism for linked drop downs.
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…

785 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