Solved

Javascript d3, get first row only

Posted on 2014-12-18
8
480 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 
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

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

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

This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
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…

695 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