Solved

Javascript d3, get first row only

Posted on 2014-12-18
8
327 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
Comment Utility
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
Comment Utility
Sorry, the column names
0
 
LVL 58

Expert Comment

by:Gary
Comment Utility
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
Comment Utility
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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
LVL 58

Expert Comment

by:Gary
Comment Utility
<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
Comment Utility
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
Comment Utility
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
Comment Utility
You are the man!
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

OverviewThis article demonstrates a simple search form using AJAX. The purpose of the article is to demonstrate how to use the same code to render a page and javascript (JQuery) and AJAX to make subsequent calls to refine the results. The princip…
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…

772 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

10 Experts available now in Live!

Get 1:1 Help Now