How to get an array from a csv column. Js - javascript

im new to js, have two question about how to parse a csv, i have a simple csv with two column, column1:'user', column2:'amount'
how can i get one array with all the 'user' column value and one
array with all the 'amount' column value?
And can i loop trough the rows and use index to get the
value of the two column? something like csv['amount'][0] or csv[0][1] or something like this?
ty.

First, declare 2 arrays, one for user, one for amount.
And then read this post to know how to read a file line by line.
After that, read this post to know how to split a string with a comma separator.
Finally, use the array.push() method to push the data into an array.

Related

Json2csv keep array value in the same column

I’m using json2csv in JavaScript.
I have a json that looks like this:
Obj:{
Key1:[“a”,”b”]
}
When I call json2csv.parse function the result is two columns. I guess it happens because the comma is the default delimiter.
I would like to keep a single column of Key1 with the array items in the same column.
I tried using transform:{unshift} but it does the opposite.
Any ideas?

How To pair and rearrange string values from a variable in PHP?

My Code
Let me summarize the above code the first variable which is Parttype stores a list of part types from the input field, the second variable stores part labels and the third variable stores BIN numbers. As you can see my output from the commented code*(Line 187)*, I to take the first values and pair them together, take the second values and pair them together, and last values to pair them together too. How would I achieve this in PHP, just to summarize my question let me attach an image of what I want.
What I want to achieve

Proper way to use split-command and ","

i have following problem:
i have an array with a lot of data retrieved from mssql and hand it over to a jsp, i will simplify it in a example:
("test","1","test2","2")
those are 4 fields. With split(",") i seperate the 4 fields, so that i can take each value and assign them to html-objects.
Now through a coincidence i found out, that if the Array is filled as follows:
("test","1","test,2","2")
where "test,2" is one text, the split command seperates the 4 values to 5 values. Is there any alternative or way so that the split command ignores the "," that are part of a string of a field?
Greetings,
Kevin
Update:
Sorry for the missunderstandings guys here i tried to simplify the code as far as i can:
<script>
var jsArray = [];
<%
// Getting ArrayList from a request Attribute with a lot of data rows
ArrayList arrayList = (ArrayList)request.getAttribute("DSList");
for(int i=0;i<arrayList.size();i++){
%>
// Pushing each row to javascript array
jsArray.push("<%= ((ArrayList)arrayList.get(i))%>");
<%
}%>
// thats the split command that gets one line of the whole dataset
Stringarray = jsArray[x].substr(1,jsArray[x].length-2).split(","); // where x is the current record
</script>
now i can simply call each filed with
Stringarray[n] //where n is the field number
thats how the code looks like, the problem now is that if in one of the Strings in any record line is a "," then the split command obviously would give back the wrong field count. I hope now it's more clear what i mean
You can use non-greedy regex to filter out value inside "" and then remove "" from the words using array#map.
var string = '("test","1","test,2","2")';
var array = string.match(/"(.*?)"/g).map(function(item){
return item.replace(/"/g,'');
});
console.log(array);
Essentially, you have a backend data source and a front-end consumer. It just so happens that they reside on the same page, since you're using JSP to generate the page and the data. So treat it like a synchronous API, just embedded into the page.
How would you transmit data between and API and JavaScript? JSON.
So, stringify your result array into a JSON string and embed that into the JSP page, then JSON.parse() that in JavaScript and iterate as you would any other array.
Since I don't see your JSP code, I can't propose a more specific solution that what's linked here for creating JSON in JSP: how to add a list of string into json object in jsp? and Creating a json object in jsp and using it with JQuery

Parse Pointer Array

How to add one to many relations in parse pointer, please check this screenshot:
It's a pointer and i can only add one pointer per row (one-to-one) i cant add one to many relationship to this using js it throws error.
{"code":111,"error":"invalid type for key members, expected *_User, but got array"}
And I don't wanna use parse 'Relation' type column due to querying 'Relation' is complicated.
If you want to have multiple-pointer column, just create array column and fill it with _User objects. Result will look like this and it will really be array of pointers:
[{"__type":"Pointer","className":"_User","objectId":"kIg9Kzzls9"},
{"__type":"Pointer","className":"_User","objectId":"TGCBZm52zW"},
{"__type":"Pointer","className":"_User","objectId":"YfGT9GvJs6"}]
Using include to get full objects in query also works, it is an array of pointers.
I have bump into this error before.
It is because the <Type> of your member column is already automatically set to *_User. If you try to set a many to many relation into the same column, it just don't work since the type are different.
You can solve the issue by manually deleting the column in your dashboard and set it in your code again.

check if any column in JSON object is undefined/null/empty

I'm new, but did run a search that yielded no results on this topic. Is there any way to check a JSON object for blanks?
Like if(json[randomInt()].hasBlanks) or something to that effect? Or do I have to ask for each column manually?
There are no columns in JSON. It's a document format not a table format. That means that every entry ("row") in an array can have any fields. Field names in one array entry can be completely different from the previous or any other. So in that sense, no, such a function does not exist. You would have to write it yourself.

Categories