I have a csv file which contains the following values:
18/10/2013, news item 1
18/10/2013, news item 2
17/10/2013, news item 3
16/10/2013, news item 4
How do I go about putting this into an array in JavaScript, ordered by date?
Once I have got it into an array, I also need to get the text values.
So far I have something like this:
Function readTextFile(){
var rawFile = new XMLhttpRequest();
Var myArray;
rawFile.open("GET", csvfile, true);
rawFile.onreadystatechange = function(){
if(rawFile.readyState === 4){
if(rawFile.Status === 200 || rawFile.Status === 0)
{
}
}
}
Sorry if the text above is not formatted properly, I am posting from my phone. thanks
This is how you can do it.
Function readTextFile(){
var rawFile = new XMLhttpRequest();
Var myArray;
rawFile.open("GET", csvfile, true);
rawFile.onreadystatechange = function(){
if(rawFile.readyState === 4){
if(rawFile.Status === 200 || rawFile.Status === 0)
{
var response = rawFile.responseText;
var splitData = new Array();
//split data with new line
splitData = response.split("\n"); //stores all the values separated by new line
console.log(splitData[0]); //returns 18/10/2013, news item 1
//split single line data with comma
var splitComma = new Array();
var splitComma = splitData[0].split(",");
console.log(splitComma[0]); //returns 18/10/2013
//start comparing date values here
}
}
}
var myArray = rawFile.responseText.split(",");
But it will not sort the data according to login date
Related
Please help me!
I have Script:
var titles =[];
titles.push('I want file txt in here');
I can not get the txt file into the titles.push, so I need some help!
function readTextFile(){
var rawFile = new XMLHttpRequest();
rawFile.open("GET", "text.txt", false);
rawFile.onreadystatechange = function (){
if(rawFile.readyState === 4){
if(rawFile.status === 200 || rawFile.status == 0){
var allText = rawFile.responseText;
console.log(allText);
}
}
}
rawFile.send(null);
}
I do not have a text file ready to show so I used what you should be reading about XMLHttpRequest.responseText you do not want to use onreadystatechange but maybe xhr.onload I left some console.log()s in the code so you can play around with it.
var titles =[];
titles.push('I want file txt in here');
var xhr = new XMLHttpRequest();
xhr.open('GET', 'https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/responseText', true);
xhr.responseType = 'text';
xhr.onload = function () {
if (xhr.readyState === xhr.DONE) {
if (xhr.status === 200) {
//console.log(xhr.response);
//console.log(xhr.responseText);
// not needed but do not want to push the entire page
// to titles so lets find just one title
var parser = new DOMParser();
var doc = parser.parseFromString(xhr.responseText, "text/html");
var title = doc.querySelector('h1');
// console.log(title);
titles.push(title);
logTitles();
}
}
};
xhr.send(null);
function logTitles() {
console.log(titles);
};
I'd like to find the javascript/Jquery equivalent of this python code but I'm unable to :
config_array = open("config.txt").read().splitlines()
This opens the text file "config.txt" and stores each line into an array while removing '\n' at the end of line. For instance :
config.txt :
first line\n
second line\n
third line\n
gives me
config_array[0] == "first line"
config_array[1] == "second line"
config_array[2] == "third line"
How to achieve the same with javascript and Jquery ? Thanks
You can do it with ajax.
function readTextFile(file)
{
var txtFile = new XMLHttpRequest();
txtFile.open("GET", file, false);
txtFile.onreadystatechange = function ()
{
if(txtFile.readyState === 4)
{
if(txtFile.status === 200 || txtFile.status == 0)
{
var result = txtFile.responseText;
result.split("\n");
console.log(result);
}
}
}
txtFile.send(null);
}
readTextFile('path to txt file')
The simplest way would be splitting the response from the file on \n see below
I assume that there are only three line in the file you are reading
$(document).ready(function () {
$.get('your_file.txt', function (response) {
a = response.split("\n");
console.log(a[0], a[1], a[2]);
})
})
You do like below
var myArray = new Array;
$.get('example.txt', function(data) {
//Bind in div
$('#div').html(data.replace('\n',''));
//insert in Array
myArray = data.split('\n');
});
The OP requires reading from a text file and removing the string '\n'. Thus,
$(document).ready(function () {
function readTextFile(file) {
const rawFile = new XMLHttpRequest();
let content = null;
rawFile.open("GET", file, false);
rawFile.onreadystatechange = function () {
if (rawFile.readyState === 4) {
if (rawFile.status === 200 || rawFile.status === 0) {
content = rawFile.responseText;
}
}
};
rawFile.send(null);
return content;
}
let textFile = 'config.txt';
// read text file
let rtf = readTextFile(textFile);
// replace string \n
let rnl = ta.replace(/\\n/g, '');
// store each line in an array
console.log(rnl.split('\n'));
}
I've been working on this code. When I run it and some fields don't exist, the error of Entity Type not defined pops up. I tried If statements but it still isn't working. I've read it may have something to do with the currency but I can't seem to figure out what. I'm a beginner and most of this isn't my code. Help! Thanks.
function customerSelected() {
var customerID = Xrm.Page.getAttribute("customerid").getValue();
var custID = customerID[0].id.substr(1, 36);
var req = new XMLHttpRequest();
req.open("GET", Xrm.Page.context.getClientUrl() + "/api/data/v8.2/accounts?$select=_defaultpricelevelid_value,paymenttermscode,shippingmethodcode&$filter=accountid eq " + custID + "&$orderby=name asc", true);
req.setRequestHeader("OData-MaxVersion", "4.0");
req.setRequestHeader("OData-Version", "4.0");
req.setRequestHeader("Accept", "application/json");
req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
req.setRequestHeader("Prefer", "odata.include-annotations=\"*\"");
req.onreadystatechange = function () {
if (this.readyState === 4) {
req.onreadystatechange = null;
if (this.status === 200) {
var results = JSON.parse(this.response);
for (var i = 0; i < results.value.length; i++) {
var _defaultpricelevelid_value = results.value[i]["_defaultpricelevelid_value"];
var _defaultpricelevelid_value_formatted = results.value[i]["_defaultpricelevelid_value#OData.Community.Display.V1.FormattedValue"];
var _defaultpricelevelid_value_lookuplogicalname = results.value[i]["_defaultpricelevelid_value#Microsoft.Dynamics.CRM.lookuplogicalname"];
var paymenttermscode = results.value[i]["paymenttermscode"];
var paymenttermscode_formatted = results.value[i]["paymenttermscode#OData.Community.Display.V1.FormattedValue"];
var shippingmethodcode = results.value[i]["shippingmethodcode"];
var shippingmethodcode_formatted = results.value[i]["shippingmethodcode#OData.Community.Display.V1.FormattedValue"];
var _defaultpricelevelid_lookupVal = new Array();
_defaultpricelevelid_lookupVal[0] = new Object();
_defaultpricelevelid_lookupVal[0].id = _defaultpricelevelid_value;
_defaultpricelevelid_lookupVal[0].name = _defaultpricelevelid_value_formatted;
_defaultpricelevelid_lookupVal[0].entityType = _defaultpricelevelid_value_lookuplogicalname;
if ("pricelevelid" != null)
{
Xrm.Page.getAttribute("pricelevelid").setValue(_defaultpricelevelid_lookupVal);
}
if ("paymenttermscode" != null)
{
Xrm.Page.getAttribute("paymenttermscode").setValue(_defaultpricelevelid_lookupVal);
}
if ("shippingmethodcode" != null)
{
Xrm.Page.getAttribute("shippingmethodcode").setValue(_defaultpricelevelid_lookupVal);
}
}
} else {
Xrm.Utility.alertDialog(this.statusText);
}
}
};
req.send();
};
Your query will always return exactly one record, so it does not make sense to use a loop to iterate the results.
Condition ("pricelevelid" != null) will always be true. It probably should be (_defaultpricelevelid_value != null). The same goes for the lines below it.
Your code is assigning _defaultpricelevelid_lookupVal to three separate attributes, which cannot be correct. Apparently this object is supposed to represent a lookup ID value referencing entity pricelevel. Therefore it only makes sense to assign it to attribute pricelevelid. paymenttermscode and shippingmethodcode are option set attributes and apparently need to be filled with the values of the equally named variables.
Replace the for loop with this code:
if (results.value[0]._defaultpricelevelid_value != null) {
var priceLevelId = [{
id = results.value[0]._defaultpricelevelid_value,
name = results.value[0]["_defaultpricelevelid_value#OData.Community.Display.V1.FormattedValue"],
entityType = "pricelevel"
}];
Xrm.Page.getAttribute("pricelevelid").setValue(priceLevelId);
}
if (results.value[0].paymenttermscode != null) {
Xrm.Page.getAttribute("paymenttermscode").setValue(results.value[0].paymenttermscode);
}
if (results.value[0].shippingmethodcode != null) {
Xrm.Page.getAttribute("shippingmethodcode").setValue(results.value[0].shippingmethodcode);
}
I'm trying store a XMLHttpRequest()'s results as a JSon String in an object, The data in the String is several arrays. I'm trying to then read through each array, in myObj.
Obviously, myObj.forEach() doesn't work, because myObj is an object, not an array or a list. How do I make it so I can itterate through myObj, and then use a forEach on each array?
Here is my current code
function getFile(){
var input = document.getElementsByName("json")[0];
var filename = input.value;
console.log(filename);
var xhr = new XMLHttpRequest();
xhr.open("GET", filename, true);
xhr.send(null);
xhr.onreadystatechange = function() {
if (xhr.readyState == 4) {
var text = xhr.responseText;
document.getElementById("displayText").innerHTML = "";
var myObj = JSON.parse(text);
myObj.forEach(function(student) {...});
}
}
}
You can grab the keys of the Object into a list using Object.keys() and then iterate through them assuming they are all lists:
function getFile(){
var input = document.getElementsByName("json")[0];
var filename = input.value;
console.log(filename);
var xhr = new XMLHttpRequest();
xhr.open("GET", filename, true);
xhr.send(null);
xhr.onreadystatechange = function() {
if (xhr.readyState == 4) {
var text = xhr.responseText;
document.getElementById("displayText").innerHTML = "";
var myObj = JSON.parse(text);
var keys = Object.keys(myObj);
keys.forEach(function(key) {
myObj[key].forEach(function(item) {...})
});
}
}
}
I have one input.txt file that have 3 columns like this:
1 0 0
2 8 8
3 272 280
4 462 742
5 702 1,444
6 990 2,434
7 1,326 3,760
8 1,691 5,451
9 2,200 7,651
10 2,640 10,291
...
I need a function (jQuery or javascript) to find a line on my input.txt and return to me the line that have the value in third column!
example:
HTML file
...
<script type="text/javascript">
var numberX = document.getElementById('id_x').value;
//FUNCTION
//var result = return of the function;
</script>
If numberX = 3,760 , the function open my file, check all last column until find the number and return 7, so result = 7
Thank you for all the help!
Try something like this:
var xhr = new XMLHttpRequest();
var data = null;
xhr.open("GET", "input.txt", true);
xhr.onreadystatechange = function() {
if (xhr.readyState == 4 && xhr.status == 200) {
data = xhr.responseText.split('\n');
data = data.map(function(row) {
return row.split(/\s+/);
});
}
};
xhr.send();
var getRecord = function(lastValue) {
return data.find(function(row) {
return row[2] === lastValue;
})[0];
};
You would use it like so:
getRecord('3,760')
First of all, you need to find a way to read your file. Try it with XMLHttpRequest.
var xhttp = new XMLHttpRequest();
xhttp.open("GET", "input.txt", false);
xhttp.send();
You real files with XMLHttpRequest object and process the result as string.
I have a quick plunker demo.
var value = '3,760';
var xmlhttp = new XMLHttpRequest();
xmlhttp.open('GET', 'data.txt');
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4) {
var text = xmlhttp.responseText;
var rows = text.split('\n');
for (var i = 0; i < rows.length; i++) {
var columns = rows[i].split(' ');
for (var j = 0; j < columns.length; j++) {
if (columns[j] == value)
alert(columns[0]);
}
}
}
}
xmlhttp.send();
https://plnkr.co/edit/5TlTltp0TaZi98IPF8Ii?p=preview