Best way to pass String array values through URL - javascript

I need to pass a string array content through URL. The String array can contain 100 - 10000 values. I got the similar question link but problem in my case it contains large amount of data.
Using comma (,) is the best way to pass the values or is there any other way to solve this problem.
I am using this in javascript function where String array contains all the selected checkbox list values.
<script type="text/javascript">
function doResend(){
var checkboxes = document.getElementsByName('selectedId');
var vals = "";
for (var i=0, n=checkboxes.length;i<n;i++) {
if (checkboxes[i].checked){
vals += ","+checkboxes[i].value;
}
}
if (vals){
vals = vals.substring(1);
window.open('resendSelectedSMS.do?smsId='+vals,"mywindow","status=1, height=335, width=400',resizable=0");
}else{
alert("Select atleast one id");
}
}
</script>

Best method is using get - post :
http://www.w3schools.com/tags/ref_httpmethods.asp
something that looks like that :
/test/demo_form.asp?name1=value1&name2=value2

Related

Retrieving data from an object

I have an object which looks like this.
{
"class_details":{
"class_1":{student_4":"<name>","student_3":"<name>,student_2":"<name>","student_1":"<name>},
"class_2":{"student_1":"<name>},
"class_0":{student_2":"<name>","student_1":"<name>
}
}
I am trying to use a loop to iterate over the classes but I am not able find a perfect way to do it.
I cant do something like this,
for(int i=0; i < $scope.rounds.class_details.length;i++)
console.log($scope.rounds.class_details.round_[i])
So i am doing this
for(int i=0; i < $scope.rounds.class_details.length;i++)
console.log(Object.keys($scope.rounds.class_details)[i])
But here the class details do not come in an order and this matters in my case.
It would be great if there is an alternative similar to
for(int i=0; i < $scope.rounds.class_details.length;i++)
console.log($scope.rounds.class_details.round_[i])
or if there is a simple way to sort the JSON class details.
To get the keys in ascending order do:
var class_details_keys = Object.keys($scope.rounds.class_details);
class_details_keys = class_details_keys.sort(function(a,b) {return (+a.substring(6)) - (+b.substring(6));});
for(int i=0, length = class_details_keys ; i < length;i++)
console.log($scope.rounds.class_details[class_details_keys[i]]);
This will return your classes in ascending order by taking the substring after 'class_' and ordering them. You can't do a simple string comparison since "4" > "10" will return incorrect result.
The simplest way to read data like you're hoping is the following:-
for(int i=0; i < $scope.rounds.class_details.length;i++)
console.log($scope.rounds.class_details["round_" + i]);
This is because you can access an objects property in two ways: -
Allows you to pass a dynamic value in (i.e. the i value)
object["property" + withVariable + "Name"]
Simple, readable way where you know the property.
object.propertyName
First, fix json. NOTE: there are some quotes missing in the original code.
"class_details":{
"class_1":{
"student_4":"<name>",
"student_3":"<name>",
"student_2":"<name>",
"student_1":"<name>"
},
"class_2":{
"student_1":"<name>"
},
"class_0":{
"student_2":"<name>",
"student_1":"<name>"
}
}
Followed to this:
//parse to json. Assume the
//'body' is the json data received
//if there is data in body
if(body){
//parse body
var parsed_body= JSON.parse(body);
//your data
var class_details = parsed_body.class_details;
var class_2_student_1 = parsed_body.class_details.class_2.student_1;
//if you want to print json directly to the front-end,
//then Object Object may be printed out. to prevent this,
//one could use .toString() to convert Object to String.
}
else{}//do something if no data in the body

How to create a two-dimensional array from textbox with comma separated values

I have a html textbox with comma seperated values for example
name,age
name,age
name,age
and so on. The structure will always be the same.
Now i want to create an array where i can retrieve a specified value like:
array[1][0] (two-dimensional)
my code so far is:
//retrieve values from textbox
var lines = $('#area').val().split(/\n/);
//create an array
var texts = []
for (var i=0; i < lines.length; i++) {
// only push this line if it contains a non whitespace character.
if (/\S/.test(lines[i])) {
texts.push($.trim(lines[i]));
}
if (/^[,]+$/.test(lines[i][i])) {
texts.push($.trim(lines[i][i]));
}
}
But this doesnt work.Does anyone have an idea how i can do this in javascript?
I am a beginner in Javascript :-)
The goal should be to get all inputed names as an array and the age as well.
Thanks in advance!!
Change this:
texts.push($.trim(lines[i]));
To this:
texts.push($.trim(lines[i]).split(","));
Fiddle

SQL + Javascript - Multiple fields into something I can loop through?

I have two fields I need to pull data from in SQL and put that into an array or list that I can loop through. Then for each loop, I do something based on both the fields for each index. What is the best method for this? I thought maybe a dictionary or possibly creating an object?
Right now I pull the fields into two seperate arrays, and I loop through both at the same time, but I am finding that sometimes one array has a blank value, and then they get out of sync and I have issues. This seems like a terrible implementation anyway.
How can I put these into a key value pair and then act on the data?
Edit: I should note that my SQL code just returns a bunch of comma seperated values. So it was easy to create an array out of those, but its proving more difficult to create anything else such as an object because I get all the values at one time.. :(
var equipIDArray = //SQL Gathering code here
var equipTypeArray = //SQL gathering code here
for(var cnt = 0; cnt < equipIDArray.length; cnt++){
alert(cnt);
if(isNaN(equipIDArray[cnt]) === true){
equipIDArray[cnt] = '';
}
switch(equipTypeArray[cnt]){
case 'Blower' :
alert('test1');
break;
case 'Dehumidifier' :
alert('test2');
break;
default :
alert('default');
}
}
It' easy to translate your arrays into an object if they just represent key/value pairs. Then you have an object you can use like a dictionary:
var equipIDArray = ["Blower","Humidifier","Lawn Mower"];
var equipTypeArray = ["Leaf blower","Whole House Humidifier","Honda Brand"];
var equipment = {};
for(var i = 0; i < equipIDArray.length; i++) {
equipment[equipIDArray[i]] = equipTypeArray[i];
}
for(property in equipment) {
console.log(property + " : " + equipment[property]);
alert(property + " : " + equipment[property]);
}

JSON rows Extracting issue in JavaScript

I have following json data coming from server in which i want to extract LimitClass and LimitClassID and store their values in respective arrays.
{
"ErrorDesc":"",
"ErrorCode":"",
"LimitClassList":"[{\"LimitClass\":\"L16\\n\",\"LimitClassId\":\"32900\\n\"},{\"LimitClass\":\"28febL0\\n\",\"LimitClassId\":\"31901\\n\"},{\"LimitClass\":\"L14\\n\",\"LimitClassId\":\"31900\\n\"},{\"LimitClass\":\"L17\\n\",\"LimitClassId\":\"32950\\n\"},{\"LimitClass\":\"L15\\n\",\"LimitClassId\":\"31950\\n\"},{\"LimitClass\":\"L0\\n\",\"LimitClassId\":\"21901\\n\"},{\"LimitClass\":\"L4\\n\",\"LimitClassId\":\"23000\\n\"},{\"LimitClass\":\"OTC Send\\n\",\"LimitClassId\":\"30901\\n\"},{\"LimitClass\":\"L2\\n\",\"LimitClassId\":\"22900\\n\"},{\"LimitClass\":\"L12\\n\",\"LimitClassId\":\"28900\\n\"},{\"LimitClass\":\"L6\\n\",\"LimitClassId\":\"23900\\n\"},{\"LimitClass\":\"L1\\n\",\"LimitClassId\":\"25900\\n\"},{\"LimitClass\":\"L13\\n\",\"LimitClassId\":\"29900\\n\"},{\"LimitClass\":\"L7\\n\",\"LimitClassId\":\"24900\\n\"},{\"LimitClass\":\"L8\\n\",\"LimitClassId\":\"26900\\n\"},{\"LimitClass\":\"L10\\n\",\"LimitClassId\":\"27900\\n\"},{\"LimitClass\":\"L13\\n\",\"LimitClassId\":\"30900\\n\"},{\"LimitClass\":\"UatTesting123\\n\",\"LimitClassId\":\"32901\\n\"}]"
}
Here is the code I have tried :
var list = data.LimitClassList;
var arrayLimitClass = [];
var arrayLimitClassId = [];
for(var i in list) {
arrayLimitClass.push(list[i].LimitClass);
arrayLimitClassId.push( list[i].LimitClassId);
}
alert(list);
alert(arrayLimitClass);
alert(arrayLimitClassId);
List variable has following result when I alert it:
[{\"LimitClass\":\"L16\\n\",\"LimitClassId\":\"32900\\n\"},{\"LimitClass\":\"28febL0\\n\",\"LimitClassId\":\"31901\\n\"},{\"LimitClass\":\"L14\\n\",\"LimitClassId\":\"31900\\n\"},{\"LimitClass\":\"L17\\n\",\"LimitClassId\":\"32950\\n\"},{\"LimitClass\":\"L15\\n\",\"LimitClassId\":\"31950\\n\"},{\"LimitClass\":\"L0\\n\",\"LimitClassId\":\"21901\\n\"},{\"LimitClass\":\"L4\\n\",\"LimitClassId\":\"23000\\n\"},{\"LimitClass\":\"OTC Send\\n\",\"LimitClassId\":\"30901\\n\"},{\"LimitClass\":\"L2\\n\",\"LimitClassId\":\"22900\\n\"},{\"LimitClass\":\"L12\\n\",\"LimitClassId\":\"28900\\n\"},{\"LimitClass\":\"L6\\n\",\"LimitClassId\":\"23900\\n\"},{\"LimitClass\":\"L1\\n\",\"LimitClassId\":\"25900\\n\"},{\"LimitClass\":\"L13\\n\",\"LimitClassId\":\"29900\\n\"},{\"LimitClass\":\"L7\\n\",\"LimitClassId\":\"24900\\n\"},{\"LimitClass\":\"L8\\n\",\"LimitClassId\":\"26900\\n\"},{\"LimitClass\":\"L10\\n\",\"LimitClassId\":\"27900\\n\"},{\"LimitClass\":\"L13\\n\",\"LimitClassId\":\"30900\\n\"},{\"LimitClass\":\"UatTesting123\\n\",\"LimitClassId\":\"32901\\n\"}]
But I am getting dots (.) when I alert arrayLimitClass and arrayLimitClassId. What am I doing wrong in extracting rows of json Object?
"LimitClassList":"[{\"LimitClass\":\"L1....]"
^ ^
LimitClassList is a string, not an array. Make it so it is an actual array, than your code should work. There should be no reason to have to parse it again.
The value below data.LimitClassList is itself a String containing JSON. You have to decode this first.
var list = JSON.parse( data.LimitClassList );
var arrayLimitClass = [];
var arrayLimitClassId = [];
// ...
This is more or less a workaround. You should have a look at your server code and fix the encoding error there!

jsons with different fields every time

I've been searching for an answer but couldn't find it.
I have a json like this:
followersperdate{
'date2012-08-29': 16823,
'date2012-07-09': 15571,
'date2012-07-07': 15528,
'date2012-06-25': 15237,
'date2012-04-19': 13283,
'date2012-03-16': 12999,
etc.
}
and I want to get the values; but everytime I make the request, I recieve other dates. I don't know how to get the values.
I've tried with jsonpath but didn't work.
This is pretty trivial using DefiantJS (http://defiantjs.com). This lib extends the global object JSON with the method "search". Using this method, you can search a JSON structure, regardless of depth for values. It returns an array with the matches (empty array if no matches were found).
To get acquainted with XPath, check out this valuable resource;
http://www.defiantjs.com/#xpath_evaluator
Check out this fiddle;
http://jsfiddle.net/hbi99/DKuAQ/
var data = {
"date2012-08-29": 16823,
"date2012-07-09": 15571,
"date2012-07-07": 15528,
"date2012-06-25": 15237,
"date2012-04-19": 13283,
"date2012-03-16": 12999
},
res = JSON.search( data, '//*' ),
str = '';
for (var i=0; i<res.length; i++) {
str += res[i] +'<br/>';
}
document.getElementById('output').innerHTML = str;

Categories