This question already has answers here:
Access Java / Servlet / JSP / JSTL / EL variables in JavaScript
(5 answers)
Closed 7 years ago.
I am using the DataTables for viewing data in table form. I am sending the data as a list of objects from the backend(JAVA). I need to retrieve the object Ids as a single string on front end and iterate on them.
For that, in iteration of the data I have concatenated the id with a separator, as
<c:set var="tagIds" value="${tag.id},${tagIds}"/>
The id String is created, what I thought - but I cant use in the JavaScript
In Javascript, I declared a variable as follows
var tagIds = ${userId};
this reflects that Syntax error. This assumes that after comma(,) it is a new variable. I am unable to pass that variable.
Also, I tried sending these values as parameter in javascript function call, but resulting in the same error.
saveTags(${userId}); & saveUploadedTags(<c:out value='${userId}'/>);
Result
Error:
SyntaxError: identifier starts immediately after numeric literal
saveTags(4028808241a34ba60141a35049380000)
You have to make sure that the resulting JavaScript code is valid, as you yourself noted:
var tagIds = "${userId}";
Now that's not necessarily the safest thing to do if you're not 100% sure of the contents of the stuff making up the id string. The best thing to do would be to use a JSON encoder; as far as I know there isn't one built into the standard JSTL/EL set of functions, but there are many available on the web.
Related
This question already has answers here:
How can I access a JavaScript object which has spaces in the object's key?
(4 answers)
Selecting a JSON object with a colon in the key
(1 answer)
How can I access object properties containing special characters?
(2 answers)
Closed 1 year ago.
I am building a site using javascript.
My data comes from a FileMaker API.
My goal is to fill an array with the data retrieved from the FileMaker API.
Except that this data comes from a linked table in FileMaker, which appends :: to the name of the variable.
Here is the code I'm trying to do, but with :: it doesn't work. How can I go about including ::?
for(var i=0;i<allFEC.length;i++)
{
programmesMovies.push({
"isOnline_b":1,
"_idWeb_c": allFEC[i].pro_FIP::_id_FEC
});
}
Anything that has spaces or any such weird syntax you can access it using square brackets
for(var i=0;i<allFEC.length;i++)
{
programmesMovies.push({
"isOnline_b":1,
"_idWeb_c": allFEC[i]["pro_FIP::_id_FEC"]
});
}
It works with spaces too. If you have pro_FIP _id_FEC as a property the same syntax will work. i.e allFEC[i]["pro_FIP _id_FEC"]
This question already has answers here:
Accessing an object property with a dynamically-computed name
(19 answers)
Closed 2 years ago.
So this is a bit of an unusual situation i am in but what i need to do is access the value of an object based on what index its stored in. The problem is that i need it in my Select component of material ui. So the overview is that i store alginment values of a video . the videos show up in a loop which means the select values are repeated and to know if its for the first video i append the index inside the object like this:
Now in order to set the option i have to access this value here :
Now the problem is in this loop i cant do something like :
halign.halign[index] because obviously that would mean its an array. So long story short how could i do something like :
value={`${halign.halign}${index}`}
So basically the end result for value to evaluate is : halign.halign0
and so on for each index.
NOTE the outer halign is the main useState object.
Remember that for JavaScript objects x['y'] and x.y are interchangeable.
So if you need to compute the key you're looking up, use:
halign['halign' + index]
Or template strings if you prefer.
Note this would be a lot easier if you organized your object with an internal array, so you could just do halign[index].
This question already has answers here:
What does "var" do in JavaScript? Why is it sometimes part of an assignment? [duplicate]
(3 answers)
Declaring javascript variables as specific types [closed]
(1 answer)
Closed 4 years ago.
I was taught to use "let" to initialize arrays in JavaScript, but I've recently discovered that "var" can be used as well.
var evenNumbers=[2,4,6,8];
I know that it's possible to initialize a function in JavaScript using var, as in
var hello=function(){};
so naturally, I assumed that the "var" being used to initialize the variable refers to the name of the array, in this case evenNumbers.
However I also recently learned that to initialize arrays in C, which I think of as the grandfather of Java-type languages, the type of variable used in the array is used to initialize the array call.
int evenNumbers[]={2,4,6,8};
Obviously in this case, int refers to the elements of the list, since an array is not an int.
I therefore assumed that var before an array call in JavaScript refers to the elements of the list. I tried to test it by applying the wrong strong type to a new JavaScript variable, like
int newYearsResolutions=["Stop procrastinating"];
Which gives me an unexpected identifier, but that's not too helpful since an array is not an int nor is "Stop procrastinating" an int. I then tried
int evenNumbers=[2,4];
and this gives me the same error, leading me back to my original conclusion that the var being named here is "evenNumbers" and not the ints 2 and 4, but I still feel like I might be missing something.
So, var evenNumbers=[2,4] appears to name the evenNumbers variable and not the elements of the array. I just want to double-check that that's the case.
JavaScript is not a typed language. int isn't a reserved word, and thus is not doing anything. var, let, const are all ways to assign variables - regardless of type.
This question already has answers here:
Accessing an object property with a dynamically-computed name
(19 answers)
Closed 6 years ago.
I have an object with some data inside. The first level of data are 2 arrays (body, cause). Each body and cause array have arrays inside of them (date, year).
totals:[{body:[
{date:[54,9,3,17]},
{year:[437,61,31,140]}]},
{cause:[
{date:[54,9,3,17]},
{year:[437,61,31,140]}]
}]
What I would like to do is access the body/cause array dynamically based on something the user has changed.
This is how I am accessing them now.
totals[isCause].body[isYear].date[filterNumber]);
My issues is body and date are hard coded in there, and I would like to have access to either body/cause date/year. I can't seem to find what these property names are stored as. I tried to set up a var and do something like this
var bodyCause = "body";
Then I tried to pass it back into my retriever statement.
totals[isCause].bodyCause[isYear].date[filterNumber]);
But that fails. So I'm just trying to figure out what that property name is stored as and if I can dynamically set it when I need to retrieve information.
Your attempt was almost correct. You can easyly use var bodyCause = "body"; and access the content dynamically. Instead of this
totals[isCause].bodyCause[isYear].date[filterNumber]);
you should use this
totals[isCause][bodyCause][isYear].date[filterNumber]);
Should fix your problem.
This question already has answers here:
How to use a variable for a key in a JavaScript object literal?
(16 answers)
Closed 7 years ago.
I have an object and a few variables I'd like nested inside the variable but for some reason I can only ever get the first nest. Anything after that gives me an error stating that it couldn't read because it was undefined:
var date = 10;
var timestamp = 100;
Que[date] = {timestamp:{"test":"test"}};//this returns {10:{"timestamp":{"test":"test"}}}; for some reason
Que[date][timestamp] = {"test":"test"}; //errors saying, cannot read '100' undefined
console.log(Que);
I'm not sure why this is happening and I'd really like to resolve it with simple means. Btw the Que variable is global inside another script and predeterminietly contains {"10":{"24":{"1":"test"}}}; which is likely why the date variable does work but the timestamp variable doesn't. Any suggestions?
EDIT:
date, and timestamp are both declared outside of the object, however i wish to use them as key's inside of the object..
uppon reading the mod suggested post and implementing what it contained, i ended up with another error
Que = {[date]:{[timestamp]:{"test":"test"}}};//this results in an unexpectd token error located at the [ before date
I think there are more questions to be asked based off of your code snippet, but in your example timesheet is not defined, you can reference the key inside your JSON by using
Que[date]["timesheet"]
To access the object inside. The key that you're looking up with the [] notation should be a string.