Add the content of a variable whose name contains :: [duplicate] - javascript

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"]

Related

Get value from JSON tree but with a number in id ('300px') [duplicate]

This question already has an answer here:
Using dot notation with number passed into function
(1 answer)
Closed 3 years ago.
I would like to get a picture link that is in my JSON tree:
I'm coding in javascript, and I'm trying to grab that data with:
data.response.cases[1].image.300px
But I have the error (In firefox) :
SyntaxError: identifier starts immediately after numeric literal
I have tried like that:
data.response.cases[i-1].image[0]
But the result is 'undefined'
Have you any idea of how to get this link ?
Thanks
First, "cases" is an object, not an array.
Second, the property "300px" starts with a number, therefore it has the be accessed using the following syntax:
data.response.cases['0'].image['300px'];

# symbol in the key in JSON is giving error while accessing it [duplicate]

This question already has answers here:
How can I access object properties containing special characters?
(2 answers)
Closed 4 years ago.
I have following JSON element.
"myObj":[
{
"#type":"xzy",
"data" :"pqr"
}
]
I am accessing a value inside the array using the key as follows
var data = myNode.filter(x => x.#type=='xyz').map(y=>y.data)
But I canot do it becuase of the # symbol in the key. I tried surrounding it with '
var data = myNode.filter(x => x.'#type'=='xyz').map(y=>y.data)
but again it fails. # symbol is valid in JSON. So, I should be able to access it. How can I do this in Javascript? Appreciate your input
Instead of:
var data = myNode.filter(x => x.'#type'=='xyz').map(y=>y.data)
Use this:
var data = myNode.filter(x => x['#type']=='xyz').map(y=>y.data)
Square bracket notation allows access to properties containing special characters and selection of properties using variables.

Accessing the #attributes of a javascript object [duplicate]

This question already has answers here:
JavaScript property access: dot notation vs. brackets?
(17 answers)
How can I access object properties containing special characters?
(2 answers)
Closed 5 years ago.
I've got some data that's getting returned from an API and converted into a js object in PHP before being passed back to the browser. One of the values i'm trying to retrieve from this object lives in the objects attributes. Here is what the object structure looks like in JS:
Currently I can access all properties I need to by calling object.comments for example or object.email.whatever.
What I can't do is access the objects attributes. Ideally I would like to get to the ID via something like object.#attributes.id but this returns an error.
Is it possible to access an objects attributes and if so how should I go about it?
Thanks
To access properties using dot notation the property must be a valid identifier. If it's not, you have to use brackets:
object['#attributes'].id

How to complete a variable name with another variable [duplicate]

This question already has answers here:
Accessing an object property with a dynamically-computed name
(19 answers)
Get global variable dynamically by name string in JavaScript
(6 answers)
Closed 6 years ago.
I have an object that stores 3 variables called text0, text1 and text2 that in turn store one string each. I also have a variable that stores the index of the clicked button, so that if I click on the third button it should fetch the text2.
I was trying something like this:
p.text(myObj.text + btnNumber);
But so far all I get is NaN. Is it even possible to do this way? What am I missing?
Use named indexing of the object to get a variable named property:
p.text(myObj["text" + btnNumber]);
Javascript lets you index properties of an object as if it were a dictionary (indexed via string names).

How to use the JSTL created variables in the JavaScript? [duplicate]

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.

Categories