combine (join) objects in array javascript - javascript

In d3.csv("file.csv", function(error, data) instruction
d3.csv is a helper that uses d3.csv.parse internally to parse CSV
data loaded from a file into an array of objects, and then it passes this
to a callback.
So data is a callback variable that holds an array of objects
In the picture the structure of data
You can see that the headers of the original CSV have been used as the property names for the data objects.Using d3.csv in this manner requires that your CSV file has a header row.
data is array of objects
data (in orange) is a combination of :
- columns array (in red) holding csv headers that means **property names**
for the **data objects**
- an array of 6131 elements (in green) holding the values associated with
these propertie
Now that we have finished describing the desired structure:
Imagine I have an array of 6131 elements (the same as described in the
picture in green)
var dataArray =[];
for(var i=0;i<6131;i++){
ddd[i]={x:..,y:...etc};
dataArray.push(ddd[i]);
}
My question is how to construct in reverse the same identical structure described before and result with the same data like the one got from d3.csv.
var columnsArray=["NOM","PRENOM","SPECIALITE","Full_Address","VILLE","lat","lon"];
Thank you very much for help.

You can attach a columns property to your dataArray object:
dataArray.columns = columnsArray;

Related

Django Trying to save my Array to the model

I have the following data coming from the
manager_item = request.POST.getlist(manager_type_item)
in my views:
['{"id":"1","title":"Bedroom"},{"id":"2","title":"Taps"}, {"id":"3","title":"Living Room"}']
Basically each one of these are {"id":"1","title":"Bedroom"} will need t be added into the model:
index = 0
for index, item in enumerate(json.loads(manager_item)):
print(item['id'])
print(item['title'])
But I not 100% sure the route to go. Hope someone can assist me. Still learning. I know how to save to the model I just need help with getting the data needed.
In fact this looks like a comma separated list of JSON blobs. We can convert this to a list as follows:
from json import loads as jsonload
data = jsonload('[{}]'.format(request.POST[manager_type_item]))
These are JSON blobs, so we can decode these with json.loads, and then process these, like:
for itm in data:
Tag.objects.create(**itm)
or in case the dictionaries contain other elements that do not belong to the model:
for itm in data:
Tag.objects.create(id=itm['id'], title=itm['title'])
We thus use a mapping to convert these JSON blobs first to there relevant vanilla Python objects (here dictionaries that map strings to strings), and then turn the given elements into a Tag (or something else).
Or in bulk:
Tag.objects.bulk_create([Tag(**itm) for itm in data])

How do you access multi-dimensional arrays of objects in JavaScript?

I need to access JavaScript objects stored in a multi-dimensional array. The data is being exported by a WordPress plug-in. Note, I cannot change the code to use a single array.
There are two arrays named "employees". Is this array format compatible with JavaScript? The JSON export was intended for PHP processing.
(Note, The code below is a simplified model to illustrate the issue).
var data = '{"employees":[{"firstName":"John0"}, {"firstName":"Anna0"},{"firstName":"Peter0"}],"employees":[{"firstName":"John1"}, {"firstName":"Anna1"},{"firstName":"Peter1"}]};';
var json = JSON.parse(data);
document.querySelector('#test').innerHTML = json.employees[2].firstName;
Here it is on JSFiddle:
https://jsfiddle.net/2524fhf4/11/
How for example, would one access the value "Peter0" in the first array? In a single array, it would be accessed like this:
var result = json.employees[2].firstName;
It appears to me that in this format it is only possible to access the last array.
It appears to me that in this format it is only possible to access the
last array.
Because when your object literal has two (or more) keys of the same name, last one will override the rest of them.
Check this demo
var data = '{"employees":[{"firstName":"John0"}, {"firstName":"Anna0"},{"firstName":"Peter0"}],"employees":[{"firstName":"John1"}, {"firstName":"Anna1"},{"firstName":"Peter1"}]}';
console.log(JSON.parse(data)); //it will only display first one
In the above example, you can see that there is only one key of the data

Reading columns in their order of the csv file

When data is loaded from a csv file, is it possible to get the order of the columns ?
E.g. the typical way to load a csv file is by calling the d3.csv function:
d3.csv("data.csv", function(error, data) {
var ageNames = d3.keys(data[0]).filter(function(key) { return key !== "State"; });
Typically, columns are stored as properties of an object, so the order cannot be retrieved anymore. Also, d3.keys() returns an array with a undefined order.
I am asking this because I want to sort the columns by the order in the csv file. Any help would be greatly appreciated.
You don't need to use the name of the column to access the data, you can use the index of the keys. The index is determined by the order in the file, so exactly what you're looking for.
To get the keys of an object, use Object.keys():
var keys = Object.keys(data[0]);
data[0][keys[0]]; // datum in the first row, first column
Coming from relational views, it seems surprising that the d3.js way of reading from a file has the opposite properties:
The relational view:
rows don't have any guaranteed order
columns have a well defined order and can be addressed by refering to their position
The d3.js view (of a csv file):
rows have a well defined order and can be addressed by refering to their row number. (they are stored in an array)
columns end up as a set of properties in an Object, thus they don't have a well defined order
If you want to access the order of columns as in the csv file, you can safely assume the "for...in.." loop over the properties or the d3.keys() returns the columns in the given order.
This order can just be destroyed if columns are added or removed. This is for most practical applications not the case.
See also https://javascriptweblog.wordpress.com/2011/01/04/exploring-javascript-for-in-loops/. The one exception described is: When column names are sole numbers, then the Chrome browser orders them according to their number order but their column order.

Get Data from Text File to Multidimensional Array Javascript

I have a little bit of an issue with a JavaScript function that needs to read data from a TextFile (something JS is already limited with) and then process tha TextFile data into a MultiDimensional Array (another thing that JS doesn't nativelly suport).
With that in mind, I have a text file in this format:
1, Name, Data, Serial
2, Name, Data, Serial
3, Name, Data, Serial
And so on.
So, the objective is to get that same data and put it, like that, into an array.
I suppose that, from what I've been reading, I need an Array of an Array, segmenting the first one by lines [/n] and the second one by commas [,]. However, given the "by-default" limitations, I'm very confused at this point. I do suppose I need jQuery, however.
I tried this:
var fs = require('fs');
var array = fs.readFileSync('file.txt').toString().split("\n");
for(i in array) {
var array = fs.readFileSync('file.txt').toString().split(",");
for(f in array) {
}
}
With little success, because then I don't really know how to store it, the objective being a Multidimensional Array that Replicates the Format of the text file, so latter it could be used to search by index or instance following an user input to get results.
I really appreciate any help.
At first glance it seems like you are trying to read in a CSV file. If that is indeed the case I recommend node-csv:
http://www.adaltas.com/projects/node-csv/
https://github.com/wdavidw/node-csv
This helped me reading file to JavaScript, however this example converts retrieved data to JSON. Just looking at the format of your text file, I would assume a JSON string or Javascript object would work with your data.
Example convert to JSON
With JSON and JS objects, instead of referencing a array indexes eg. array[i][x]. you would replace [x] with .propertyName
data = {
"id": 1,
"name": "Fred"
};
//access data like this
data[i].name //will return "Fred" as i =0
to create JS object, just initialize array properties without the ""(quotation marks). accessing JS and JSON properties are done in the same way, main advantage over a multidimensional array is that you can reference an actual property name, as opposed to indexes.

ng-repeat for object inside array of arrays

I am receiving an array of string arrays from my back-end. (Java - CXF-RS)
The size of an array in the string arrays vary from 1 to n. To read and populate it in my front-end, I am using ng-repeat.
Everything works fine except for one condition. If any of the string array has only one data in it, it is received as an Object. Hence ng-repeat is failing for those conditions.
Is there a work-around to fix this?
The best option would be to force the server to return an Array every time. If that is not possible, you can check if the returned data is an array using:
angular.isArray()
https://docs.angularjs.org/api/ng/function/angular.isArray
and if it is not, then push the data onto the array that the ngRepeat is using.
Pseudo code:
1. Get data from server
2. In success callback initialize your array
3. If returned data is array, assign it to your array
4. if not, then push the data onto your array.
$http.get('url', function(data) {
$scope.items = [];
if(angular.isArray(data) {
$scope.items = data;
} else {
$scope.items.push(data);
}
});
I havent checked this bit of code for syntax errors or linted it. This was just to give you an idea.

Categories

Resources