How to pass an array of string in a url - javascript

Hello i have an url in my jsp and i want to pass an array of string in this url to recover in my ActionForm

If you are dealing with something simple like a list of numeric ids, i would just run through the check boxes, create a comma separated list, and assign it to a query string parameter. On the other side i would split the string.
If the values are more complex you have to consider escape characters. Also if you are dealing with a long list, the url is not the best way to pass this data.

You can use 'standard' html way of passing arrays of data: http://mywebsite/mypage?myarray=value1&myarray=value2&myarray=value3. Then you can fetch all values of parameter myarray from request object (if framework doesn't provide more elegant ways of handling arrays).
But seeing your comment, I would recommend to leave JavaScript and just declare a form for it.
If you need a link (not button), you can always submit form from it. Something like ...

Try Json encode
http://code.google.com/p/json-simple/
Check this

Related

Don't allow values ​other than the one defined to be added. MongoDB

I have a document, called QuestionSchema, which has a field called "standardAnswer", I would like to have the possibility to let this field receive only the values that I have allowed it to receive.
For example, it would be "positive", "negative"...
Is there any option in the Schema creation or anything that I could do to achieve that?
I think you're looking for enums/schema validation.
Try this: https://mongoosejs.com/docs/validation.html
No, MongoDb does not support tuples. Many types are supported, the most common ones being:
Double
String
Object
Array
Binary data
Boolean
Date
Null
Regular Expression
You will have to create a method or function which would only allow db operations in the cases you set.

Access Array with String key

I have two variables with JSON files. The first is a list of keys looks like this:
keylist = ["key1","key2","key3"]
The second one is generated from a database and looks like this:
data = {
"key1"{
#further data
},
"key2"{
#further data
},
"key3"{
#further data
}
}
Now I want to access the second element of the database with the key from the keylist
data.keylist[1];
Which doesn't work because the return of keylist[1] is a String? I did some research and the use of the window function was proposed. So I tried this:
window["data." + keylist[1]]();
Which leads to a "is not a function" error. What can I do to solve this problem?
As simple as that:
const mydata = data[ keylist[1] ];
Also, your code is correct from the point of syntax, but it tells completely different than you expect it to tell.
data.keylist[1];
tells JS that you expect to have an object called data which has a property called keylist and which is (most likely) type of array, and you want to get the second element of this array.
PS: And one more point here. Your question title is not completely correct because of the difference between Arrays and Object in JS.
There is no "string keys" for arrays in JS, so you cannot "access array with a string key". Well, truly speaking there are, but not for items of array. Array items only have numeric index, which you can use to access it. Objects, in contrast to arrays, may have named properties. So when you see something like that: data = myVar['data'], you can tell that you're dealing with an object, while data = someVar[0] can be both, an Array (most likely) or also an Object with key named '0'.
I don't think the issue you're having with your first example is because it returns a key. I believe the issue is because data doesn't have a property called keylist. Instead of that, try it as
data[keylist[1]]
and see if that works for you. The reason this one should work is that, in this situation, Javascript will evaluate the string return of keylist[1] and then use it as a string index for the data variable. Let me know if this works out for you :D
You can try using using something like this.
data[keylist[1]]

Angular2: Passing multilevel object as route params

I am working on a data list page that uses some filters (some select boxes with multiple value selection)
I need to be able to deep link this results so I need to pass all the params to the URL
if I pass a multilevel object (and not just key:value) pairs, I get [object Object] in the URL which is not good.
what would be the correct way to handle this? JSON.stringify?
Yes JSON.stringify and How to encode a query string so that it is the value of another query string in javascript? to make it a valid query value.
See also https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/encodeURIComponent

accessing an object property that contains forward slashes

I have created JSON by using the json_encode PHP function. The key of one of the items of the array contains a forward slash and when the JSON is parsed, the object looks like this when output in Chrome's console.
Object
contact/allow_anonymous: "0"
menulayout: "horizontal"
pages/max_pages: "10"
primarycolour: "329e95"
websitelogo: "text"
My problem is that I can't seem to be able to access the value of the properties that have a forward slash in them.
Any ideas? Since javascript allowed me to create the object I would assume there is a way to retrieve the values.
Just use myObject["key"] instead of myObject.key:
alert(myObject["contact/allow_anonymous"]);
Just replace the forward slash with ~1.
Instead of contact/allow_anonymous use contact~1allow_anonymous

How could I add new property into a JSON string in JavaScript?

By JSON text, I mean the return value of JSON.stringify. I know how to do this with JSON object, but I couldn't figure out how to do this with JSON text (add new attribute/element, say "sn":"1" to JSON text, but its structure is kept and I don't need to stringify it again), can anyone help me?
Thanks!
I don't know why you'd want to do this - why not just add the property before you stringify it?
But if you must, given a string that contains JSON:
var myJSON = '{"prop1":"val1","prop2":"val2"}';
You can easily add a property to the beginning by doing this:
myJSON = '{' + '"sn":"1",' + myJSON.substr(1);
Or add it to the end:
myJSON = myJSON.replace(/}$/, ',"sn":"1"' + '}');
Or use whatever other combination of String manipulation functions takes your fancy...
If you want to add the new property in a specific place within the string, say inside a nested object or array or something, well, again some kind of regex or combination of .indexOf() and .substr() or something could do it, but really I think it's nuts to approach it this way.
Obviously the above code can be wrapped up in a function, and '"sn":"1"' can be replaced with a parameter or variable name or whatever - but why?
Note also that I've assumed above that there will be at least one existing property and inserted a comma accordingly- up to you to make that smarter if you want to allow for empty objects.
P.S. There aren't "JSON strings" and "JSON objects": all JSON is a string. In JavaScript one way of creating objects is with the object literal syntax that inspired JSON, but there's no such thing as a JSON object.
It makes no sense to do it the way you're suggesting... just turn it back into an Object, add your field and stringify it again! Or am I missing something?
You're going to have to parse it somehow. The most straightforward way is probably un-stringifying it to object/array/literal data. But if you don't want to do that, you could either use regular expressions, or methods of the String object like substr to manipulate the string directly.

Categories