I'm writing a program that takes in some data from a file (in this case, a list of IDs).
This program then takes the IDs, interfaces with a weather server, grabs the information the server kicks back, and parses it.
It then sorts the data in order of name by city, and pushes it into an array.
I'm trying to get it printed out, but when I print the array, I keep getting the following output:
[ { string: 'Dallas, TX : 91' },
{ string: 'Houston, TX : 86' },
{ string: 'Houston, TX : 86' },
{ string: 'Jacksonville, FL : 83' },
{ string: 'Laguna Hills, CA : 67' },
{ string: 'New York, NY : 91' },
{ string: 'Seattle, WA : 62' } ]
Naturally, I anticipate having the square brackets included, and the commas as well. However, why is it printing out the "string:" and curly braces?
Here is my source:
var XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest;
var lineReader = require('line-reader');
var cityList = [];
var numItems = 0;
var needToPrint = 0;
lineReader.eachLine('IDList.txt', function(line, last) {
numItems++;
getResponse(line, textParse);
});
var getResponse = function(id, callback){
var request = new XMLHttpRequest;
request.open("GET", "http://weather.yahooapis.com/forecastrss?w=" + id +"&u=f");
request.onreadystatechange = function(){
if(request.readyState === 4 && request.status === 200){
var type = request.getResponseHeader("Content-Type");
if(type.indexOf("xml") !== -1 && request.responseXML)
callback(request.responseXML);
else if(type === "application/json")
callback(JSON.parse(request.responseText));
else
callback(request.responseText);
}
};
request.send(id);
}
var textParse = function (input)
{
var index = input.indexOf("city=\"") + "city=\"".length;
var endIndex = input.indexOf("\" region=\"");
var city = input.substring(index, endIndex);
index = input.indexOf("region=\"") + "region=\"".length;
var state = input.substring(index, index + 2);
index = input.indexOf("temp=\"") + "temp=\"".length;
endIndex = input.indexOf("\"", index);
var temp = input.substring(index, endIndex);
var obj = new location(city, state, temp);
cityList.push(obj);
cityList.sort(sortFunc);
needToPrint++;
if(numItems === needToPrint)
printData(cityList);
}
var location = function (city, state, currentTemp)
{
this.string = city + ", " + state + " : " + currentTemp;
};
var sortFunc = function(input1, input2)
{
if (input1.string < input2.string) //sort string ascending
return -1
if (input1.string > input2.string)
return 1
return 0 //default return value (no sorting)
}
var printData = function(objectList){
console.log(objectList);
}
You're creating a location object:
var obj = new location(city, state, temp);
In that object, you create a string property:
this.string = city + ", " + state + " : " + currentTemp;
If what you want is a simple array of strings, change textParse to do this:
cityList.push(city + ", " + state + " : " + currentTemp);
(instead of pushing a location object)
This would require you to rewrite the sort function too.
It looks like you didn't write that code, otherwise you would understand it. Maybe what you're missing is that objectList is an array of objects. You can access your data by array index, then object property (in this case, string). For example, try this in printData:
console.log(objectList[1].string); // 'Houston, TX : 86'
For further info on how to traverse your data, see Access / process (nested) objects, arrays or JSON
It looks like you're pushing Javascript objects into the cityList array, so when you call printData(cityList), you're logging an array object, not a string. So, the console spits out JSON. If you want raw text, you need to build a string.
Related
EDIT - code to calculate refill_playlist_len included
I have a function in Javascript that deletes a row of an HTML table and populates it again with values from arrays.
Within this deleteRow function, I have a for loop which loops through a string and assigns parts of the strings to different variables and tries to push them onto arrays.
Without the for loop, it works fine (i.e. when I just index manually) but for some reason when I place it in a for loop, the values aren't pushed onto the arrays. The values themselves print fine on each iteration they just aren't added to the array.
Refill_playlist_len is the count of the Django Queryset (30).
var refill_playlist_len = '{{ playlist.count }}';
var artist_Arr = [];
var track_Arr = [];
var track_id_Arr = [];
var album_Arr = [];
var artist_name;
var track_name;
var track_id;
var album_name;
for (var i = 0; i < refill_playlist_len; i++) {
var searchStr = refill_playlist[i];
console.log(searchStr);
console.log(typeof searchStr);
console.log(typeof refill_playlist);
//grab variables
artist_name = searchStr.match(new RegExp("artist_name:" + "(.*)" + ", album_name:"));
console.log(artist_name[1]);
artist_Arr.push(artist_name[1]);
track_name = searchStr.match(new RegExp("track_name:" + "(.*)" + ", acousticness:"));
console.log(track_name[1]);
track_Arr.push(track_name[1]);
track_id = searchStr.match(new RegExp("track_id:" + "(.*)" + ", track_name:"));
console.log(track_id[1]);
track_id_Arr.push(track_id[1]);
album_name = searchStr.match(new RegExp("album_name:" + "(.*)" + ", track_number:"));
console.log(album_name[1]);
album_Arr.push(album_name[1]);
}
The console logs are in the image below. You can see part of the 'searchStr' printed, along with the data types, artist name, track IDs, etc but for some reason, it says that 'searchStr' is undefined?
Console
I'm quite new to Javascript so my apologies if there is something basic I'm forgetting.
Multiple issues with code. Please clean up code. Sample is given below.
function find(refill_playlist) {
const refill_playlist_len = refill_playlist.length
let artist_Arr = []
let track_id_Arr = []
let track_Arr = []
let album_Arr = []
for (i = 0; i < refill_playlist_len; i++) {
var searchStr = refill_playlist[i];
if(!searchStr) continue;
//grab variables
artist_name = searchStr.match(/artist_name:(.*), album_name:/);
artist_name && artist_Arr.push(artist_name[1]);
track_name = searchStr.match(/track_name:(.*), acousticness:/);
track_name && track_Arr.push(track_name[1]);
track_id = searchStr.match(/track_id:(.*), track_name:/);
track_id && track_id_Arr.push(track_id[1]);
album_name = searchStr.match(/album_name:(.*), track_number:/);
album_name && album_Arr.push(album_name[1]);
}
console.log(artist_Arr)
console.log(track_id_Arr)
console.log(track_Arr)
console.log(album_Arr)
}
find(
[
`
artist_name: test, album_name:
`,
null
]
)
Can someone please help me to write a JS method which takes a String value like
/Content/blockDiagram/0/bundle/0/selectedBundle
/Content/blockDiagram/1/bundle/1/selectedBundle
/Content/blockDiagram/0/bundle
and convert it to
/Content/blockDiagram[1]/bundle[1]/selectedBundle
/Content/blockDiagram[2]/bundle[2]/selectedBundle
/Content/blockDiagram[1]/bundle
It is basically taking the number in the path and increment it by 1 and then changing the structure of the string.
My attempt
function setReplicantPartListOptions(list) {
list = "/" + list;
var index = list.lastIndexOf("/");
var tempString = list.substring(0, index);
var index2 = tempString.lastIndexOf("/");
var initialString = list.substring(0, index2);
var result = tempString.substring(index2 + 1, index) var middlevalue = parseFloat(result) + 1
var lastString = list.substring(index, list.length);
list = initialString + "[" + middlevalue + "]" + lastString;
return list;
}
simple regular expression with capture group with replace
var str = "/Content/blockDiagram/0/bundle/0/selectedBundle"
var updated = str.replace(/\/(\d+)/g, function (m, num) {
var next = +num + 1; // convert string to number and add one
return "[" + next + "]"; //return the new string
})
console.log(updated)
String.replace(RegExp, callback(match, contents)) is the callback version of String.replace().
In my case, the first parameter of callback function is the result/match. It takes the match and converts it to number using + operator, and then increment it by one. Finally, I add [ ] around the value and return it!
let str = "/Content/blockDiagram/0/bundle/0/selectedBundle"
console.log(
str.replace(/\b\d+\b/g, match => `[${ +match + 1 }]`)
);
var str = "/Content/blockDiagram/0/bundle/0/selectedBundle"
console.log(
str.replace(/\/(\d+)\//g, function(_,num) { return `[${++num}]`})
)
In the below code Im not getting the right result. How can I can do pattern match in javascript?
function getPathValue(url, input) {
console.log("this is path key :"+input);
url = url.replace(/%7C/g, '|');
var inputarr = input.split("|");
if (inputarr.length > 1)
input = '\\b' + inputarr[0] + '\n|' + inputarr[1] + '\\b';
else
input = '\\b' + input + '\\b';
var field = url.search(input);
var slash1 = url.indexOf("/", field);
var slash2 = url.indexOf("/", slash1 + 1);
if (slash2 == -1)
slash2 = url.indexOf("?");
if (slash2 == -1)
slash2 = url.length;
console.log("this is path param value :"+url.substring(slash1 + 1, slash2));
return url.substring(slash1 + 1, slash2);
}
getPathValue("http://localhost/responsePath/mountainwithpassid|accesscode/100/mountainwithpassid|passid/1","mountainwithpassid|passid")
Im getting the below output
If I pass mountainwithpassid|accesscode as input Im getting output as
100. Same way if I pass
key :mountainwithpassid|passid value :100 // Expected output 1
If your intention is to simply retrieve the value in the path that follows the input (contained within '/') then you can achieve this with a simpler regular expression. First you will need a method to escape your input string since it contains a pipe character '|' which is translated as OR in regex.
You can use this (taken from https://stackoverflow.com/a/3561711):
RegExp.escape= function(s) {
return s.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&');
};
Then your getPathValue function can look something like:
function getPathValue(url, input) {
var pathValue = null;
var escapedInput = RegExp.escape(input);
// The RegExp below extracts the value that follows the input and
// is contained within '/' characters (the last '/' is optional)
var pathValueRegExp = new RegExp(".*" + escapedInput + "/([^/]+)/?.*", 'g');
if (pathValueRegExp.test(url)) {
pathValue = url.replace(pathValueRegExp, '$1');
}
return pathValue;
}
You will also need to think about how you handle errors - in the example a null value is returned if no match is found.
I'm trying to understand the question. Given a URL of:
"http://localhost/responsePath/mountainwithpassid|accesscode/100/mountainwithpassid|passid/1"
and an argument of:
"mountainwithpassid|passid"
you expect a return value of:
"1"
An argument of
"mountainwithpassid|accesscode"
should return:
"100"
Is that correct? If so (and I'm not certain it is) then the following may suit:
function getPathValue(url, s) {
var x = url.indexOf(s);
if (x != -1) {
return url.substr(x).split('/')[1];
}
}
var url = "http://localhost/responsePath/mountainwithpassid|accesscode/100/mountainwithpassid|passid/1";
var x = "mountainwithpassid|passid";
var y = "mountainwithpassid|accesscode";
console.log(getPathValue(url, x)); // 1
console.log(getPathValue(url, y)); // 100
I am trying to replace values in a string with the comparable jSON property in an object.
var value = "/sessions/:sessionId/events/:id";
var result = replace(value, { sessionId : 1 , id : 23 });
// result : "/sessions/1/events/23"
console.log(result);
Is it possible with JavaScript (I'm sure it is)? Not sure about the most efficient way to do this and how to handle it when all the values inside the template string are not matched.
Thanks in advance.
Update (Solution)
var url = function (template, parameters) {
var extra = [];
for (param in parameters) {
if (value.indexOf(param) < 0) {
extra.push(param + '=' + parameters[param]);
}
}
var result = template.replace(/:(\w+)/g, function (substring, match) {
var routeValue = parameters[match];
if (!routeValue) {
throw "missing route value for " + match + ' in "' + template +'"';
}
return routeValue;
});
if (result.indexOf("/:") > 0) {
throw "not all route values were matched";
}
return (extra.length === 0) ? result : result + "?" + extra.join("&");
};
var value = "/sessions/:sessionId/events/:id";
var data = {
sessionId: 1,
id: 23,
term: "butter"
};
// result : /sessions/1/events/21?term=butter
console.log(url(value, data));
A regex would work just fine here.
var value = "/sessions/:sessionId/events/:id";
var obj = { sessionId : 1 , id : 23 };
var result = value.replace(/:(\w+)(\/|\b)/g, function(substring, match, nextMatch){
return obj[match] + nextMatch;
});
Assuming you have the following javascript object:
var myObject = { sessionId : 1 , id : 23 };
you can loop each property and do a replace on the original string...
var value = "/sessions/:sessionId/events/:id";
for(var item in myObject){
value = value.replace(item, myObject[item]);
}
//value = "/sessions/:1/events/:23"
Here is a working example
It is not clear if you want to keep the : characters or not. If not, then you can just include that in your replace function:
value = value.replace(':' + item, myObject[item]);
Checking for missing parameters
If you have any extra values in your object that do not exist in your input string, then they will not have any effect. If however, you want to take action if one of the items is not found in the original string, then you can do a check in the loop:
var noMatch = false;
for(var item in myObject){
if(value.indexOf(item) < 0){
noMatch = true;
}
value = value.replace(item, myObject[item]);
}
if(noMatch){
//something is wrong!
}
Dealing with JSON
If you do actually have a JSON string to begin with, you can convert that to an object with the following:
var jsonString = '{"sessionId": 1, "id": 23}';
var myObject = JSON.parse(jsonString);
here is another way of doing it.
http://jsfiddle.net/rayweb_on/suHej/
var value = "/sessions/:sessionId/events/:id";
var source = { sessionId : 1 , id : 23 };
var result = value.replace(":sessionId",source.sessionId);
var result = result.replace(":id",source.id);
console.log(result);
I need to take a query string (example: ?name=value1&type=value2&name=value3&type=value4... but this code should be able to handle any query string passed to it) and create an object from it.
I have nearly completed a function that does this. However, my logic is a bit off inside that last for loop. If someone could please point out where I've gone wrong I'd really appreciate it!
First I grab the query string using jQuery's $.get function:
$.get("xmlreader.php", function(data) {
var queryParams = [];
var qString = data;
// test qString for & at the end
var last = qString.lastIndexOf("&");
if(last == qString.length - 1) {
// then there is a & to remove
qString = qString.substring(0,qString.length-1);
}
getParamObj(qString);
});
(I know it's funky that I remove the last & here in my JavaScript but I don't want to focus on that here at all.)
Then I call getParamObject(qString); to build the actual collection object:
function getParamObj(parameterString) {
var qString = parameterString;
var parameters = qString.split("&");
parameters[0] = parameters[0].substring(1);
var paramObject = new Object();
for(var index = 0; index < parameters.length; ++index) {
var equalsPos = parameters[index].indexOf("=");
var key = parameters[index].substring(0,equalsPos);
var stringLength = parameters[index].length;
var value = parameters[index].substring(equalsPos + 1, stringLength);
if(!paramObject[key]) {
console.log("paramObject[key] = " + paramObject[key]);
paramObject[key] = value;
if(paramObject[key] instanceof Array ) {
console.log("instance of array");
paramObject[key].push(value);
} else {
var newArray = [];
var existingValue = paramObject[key];
console.log("existing value: " + existingValue);
console.log("value: " + value);
newArray.push(existingValue);
newArray.push(value);
paramObject[key] = newArray;
}
}
}
The function is working -almost- correctly. A couple things are wrong when I test it: in the last conditional existingValue and value are the same. There may be other issues as well but I think that's the primary one.
I've seen approaches that use regular expressions but I DON'T want to take that route here.
Note: I've answered my own question LOL... stack won't let me comment this because I'm not cool enough yet at just 58 rep :)
[code]
if(!paramObject[key]) {
console.log("paramObject[key] = " + paramObject[key]);
paramObject[key] = value;
console.log("paramObject[key] = value = " + value + paramObject[key]);
console.log(paramObject[key]);
}
else if(paramObject[key] instanceof Array ) {
paramObject[key].push(value);
console.log("value if arra" + value);
} else {
var newArray = [];
var existingValue = paramObject[key];
console.log("existing value: " + existingValue);
console.log("value: " + value);
newArray.push(existingValue);
newArray.push(value);
paramObject[key] = newArray;
}
[/code]
Although you say you don't want a regex-based implementation, I thought it would be helpful to show what it can look like:
params = {}
queryString.replace(/([^&=]+)=([^&=]*)/g, function($0, $1, $2) {
(params[$1] = params[$1] || []).push($2);
});
// that's basically all about this
(Doesn't answer the question, hence CW).