how to create json array without keys - d3.js

{"":[{"aprjun":"50","janmar":"31","julsep":"42","octdec":"7","year":"2010"},
{"aprjun":"19","janmar":"6","julsep":"15","octdec":"68","year":"2011"},
{"aprjun":"16","janmar":"4","julsep":"12","octdec":"14","year":"2012"},
{"aprjun":"97","janmar":"9","julsep":"36","octdec":"157","year":"2013"},
{"aprjun":"","janmar":"11","julsep":"","octdec":"","year":"2014"}]}
i am having an json array like this which i need to give input to d3js bt i want it like this
[{"aprjun":"50","janmar":"31","julsep":"42","octdec":"7","year":"2010"},
{"aprjun":"19","janmar":"6","julsep":"15","octdec":"68","year":"2011"},
{"aprjun":"16","janmar":"4","julsep":"12","octdec":"14","year":"2012"},
{"aprjun":"97","janmar":"9","julsep":"36","octdec":"157","year":"2013"},
{"aprjun":"","janmar":"11","julsep":"","octdec":"","year":"2014"}]
how do i get it?

Imagine your json data is like this:
var k = {"":[{"aprjun":"50","janmar":"31","julsep":"42","octdec":"7","year":"2010"},{"aprjun":"19","janmar":"6","julsep":"15","octdec":"68","year":"2011"},{"aprjun":"16","janmar":"4","julsep":"12","octdec":"14","year":"2012"},{"aprjun":"97","janmar":"9","julsep":"36","octdec":"157","year":"2013"},{"aprjun":"","janmar":"11","julsep":"","octdec":"","year":"2014"}]}
so you need to do : k[""] to give you the desired JSON
[{"aprjun":"50","janmar":"31","julsep":"42","octdec":"7","year":"2010"},{"aprjun":"19","janmar":"6","julsep":"15","octdec":"68","year":"2011"},{"aprjun":"16","janmar":"4","julsep":"12","octdec":"14","year":"2012"},{"aprjun":"97","janmar":"9","julsep":"36","octdec":"157","year":"2013"},{"aprjun":"","janmar":"11","julsep":"","octdec":"","year":"2014"}]

Use array indexing. for (var i=0; i < 5; i++) { array [i] = {...} } where 'array' is the name of your variable. Incidentally, why not use the year as a key?

Related

Java8 stream average of object property in collection

I'm new to Java so if this has already been answered somewhere else then I either don't know enough to search for the correct things or I just couldn't understand the answers.
So the question being:
I have a bunch of objects in a list:
try(Stream<String> logs = Files.lines(Paths.get(args))) {
return logs.map(LogLine::parseLine).collect(Collectors.toList());
}
And this is how the properties are added:
LogLine line = new LogLine();
line.setUri(matcher.group("uri"));
line.setrequestDuration(matcher.group("requestDuration"));
....
How do I sort logs so that I end up with list where objects with same "uri" are displayed only once with average requestDuration.
Example:
object1.uri = 'uri1', object1.requestDuration = 20;
object2.uri = 'uri2', object2.requestDuration = 30;
object3.uri = 'uri1', object3.requestDuration = 50;
Result:
object1.uri = 'uri1', 35;
object2.uri = 'uri2', 30;
Thanks in advance!
Take a look at Collectors.groupingBy and Collectors.averagingDouble. In your case, you could use them as follows:
Map<String, Double> result = logLines.stream()
.collect(Collectors.groupingBy(
LogLine::getUri,
TreeMap::new,
Collectors.averagingDouble(LogLine::getRequestDuration)));
The Collectors.groupingBy method does what you want. It is overloaded, so that you can specify the function that returns the key to group elements by, the factory that creates the returned map (I'm using TreeMap here, because you want the entries ordered by key, in this case the URI), and a downstream collector, which collects the elements that match the key returned by the first parameter.
If you want an Integer instead of a Double value for the averages, consider using Collectors.averagingInt.
This assumes LogLine has getUri() and getRequestDuration() methods.

Processing: How can I find the number of times two fields are equal in a CSV file?

I'm learning Processing for the first time and I've been tasked to deal with data but it's been terribly confusing for me.
For every line of a CSV file (apart from the header), I want to compare two specific columns of each. i.e. ListA vs ListB
For example, with the data below:
ListA,ListB
Male,Yes
Male,No
Female,Yes
Male,Yes
And for example, I want to check for all instances that a value in ListA is "Male" AND that the corresponding value in ListB is "Yes". In this scenario, I should get the value "2" for the two rows this is true.
How would I do that?
For now, I have a 2D String array of the data in the CSV file. From that I managed to assign specific columns as ListA and ListB. I tried using sort but it would only sort one list and not both at the same time.
Current relevant code:
for (int i=1; i<lines.length; i++) {
listA[i-1] = csv[i][int(whichA)];
listB[i-1] = csv[i][int(whichB)];
}
lA = Arrays.asList(listA);
lB = Arrays.asList(listB);
Not sure if this code really helps makes things clearer though. :/
Any help would be appreciated. Thank you.
So something like this should do what you need it to. Pseudocode:
int numRows = 0;
for (int i = 0; i < length; ++i) {
if (array1[i] equals "Male" AND array2[i] equals "Yes") {
++numRows;
//add to new collection here if you need the data
}
}

Get,Set Operation on Arrays

How to use Get Set properties on arrays in c# scripting.In order to set particular index and to get the value what is the procedure should be followed?
I am using Test complete software and c#scripting.
TestComplete's C#Script is actually JScript with square bracket notation instead of dot notation. You can manipulate arrays in C#Script in the same way as in JScript (or JavaScript, which is similar):
// Define an array using an array literal
var names = ["Alice", "Bob", "Charlie"];
Log.Message(names[0]); // "Alice"
// Create and populate an array using a loop
var numbers = new Array(); // or: var numbers = [];
for (var i = 0; i < 5; i++) {
numbers[i] = i;
}
Log.Message(numbers.join()); // "0,1,2,3,4"

Get query string in Google CSE v2

I am using Google CSE v2, and I need to get the query that the user entered. The problem is that it is ajax, and the query is not in the url.
Does anyone know a solution?
Thanks
First off, when you create the search box, you need to give it a 'gname' attribute so you can identify it in your javascript, like so:
<gcse:searchbox gname="storesearch"></gcse:searchbox>
<gcse:searchresults gname="storesearch"></gcse:searchresults>
Or, if you're using the html5 style tags (which you should unless you have a reason not to):
<div class="gcse-searchbox" data-gname="storesearch"></div>
<div class="gcse-searchresults" data-gname="storesearch"></div>
(Replace 'storesearch' with whatever name you want to use to identify this custom search.)
More info on that here: https://developers.google.com/custom-search/docs/element#supported_attributes
Then, you can access the custom search element and get the current query like so:
var cseElement = google.search.cse.element.getElement('storesearch'),
query = cseElement.getInputQuery();
or if you don't need the reference to the element anymore, obviously that could be combined into one line:
var query = google.search.cse.element.getElement('storesearch').getInputQuery();
The docs for that part are here: https://developers.google.com/custom-search/docs/element#cse-element
I know this is already answered correctly. But for those also looking for a simple JS function to achieve this, here you go. Pass it the name of the variable you want to extract from the query string.
var qs = (function(a) {
if (a == "") return {};
var b = {};
for (var i = 0; i < a.length; ++i) {
var p=a[i].split('=');
if (p.length != 2) continue;
b[p[0]] = decodeURIComponent(p[1].replace(/\+/g, " "));
}
return b;
})(window.location.search.substr(1).split('&'));

LINQ: Field is not a reference field

I've got a list of IQueryable. I'm trying to split this list into an array of IQueryable matching on a certain field (say fieldnum) in the first list...
for example, if fieldnum == 1, it should go into array[1]. I'm using Where() to filter based on this field, it looks something like this:
var allItems = FillListofMyObjects();
var Filtered = new List<IQueryable<myObject>(MAX+1);
for (var i = 1; i <= MAX; i++)
{
var sublist = allItems.Where(e => e.fieldnum == i);
if (sublist.Count() == 0) continue;
Filtered[i] = sublist;
}
however, I'm getting the error Field "t1.fieldnum" is not a reference field on the if line. stepping through the debugger shows the error actually occurs on the line before (the Where() method) but either way, I don't know what I'm doing wrong.
I'm farily new to LINQ so if I'm doing this all wrong please let me know, thanks!
Why don't you just use ToLookup?
var allItemsPerFieldNum = allItems.ToLookup(e => e.fieldnum);
Do you need to reevaluate the expression every time you get the values?
Why not use a dictionary?
var dictionary = allItems.ToDictionar(y => y.fieldnum);

Resources