why can't i have a variable in an object? I get an error like:
"Uncaught SyntaxError: Unexpected token this"
My code is like this.
$("#search_options input:checkbox").on('click', function() {
var params = {
$(this).attr('name') : $(this).val(),
};
var str = jQuery.param(params);
});
I'm sure that $(this) is working because I tried to console.log it outside the params object then i is working.
Object literals cannot have variable property names. You'll have to assign the property like so:
...
var params = {};
params[$(this).attr('name')] = $(this).val();
var str = jQuery.param(params);
If you want to use value of a variable as a property name, you must use this syntax:
var params = {}
params[$(this).attr('name')] = $(this).val();
The literal notation, that you're trying to use, expects property name to be a valid JavaScript identifier.
Related
I would like to assign a JSON value to var, however i get the following error.
Error message
var a = { myData.results[count].category[count] }
Maybe a syntax error, either make it a property of an object:
var a = { prop: myData.results[count].category[count] };
Or just the value:
var a = myData.results[count].category[count];
This question already has answers here:
How do I create a dynamic key to be added to a JavaScript object variable [duplicate]
(2 answers)
Closed 5 years ago.
I am creating a URL Parser for a school project. I first parse the full query from the url into an array, since query members are separated by "&".
var queryString = /\?(\&?\w+\=\w+)+/.exec(urlString))[0].split("&");
Each array member would look like:
arr[i] = "field=value";
I am doing this in an Object constructor in which I need to return an "HTTPObject". For the assignment, the HTTPObject must have a property called "query", and query should have many properties (one for each query field in the url). Why doesn't this work?
queryString.forEach(function(element) {
var elementArr = element.split("=");
this.query.elementArr[0] = elementArr[1];
});
you cannt set a property like that - but you can with bracket notation:
try this
queryString.forEach(function(element) {
var elementArr = element.split("=");
this.query[elementArr[0]] = elementArr[1];
});
You were very close. You can refer to object properties by name, using square brackets...
var queryString = ["name1=value1", "name2=value2"];
var query = {};
queryString.forEach(function(element) {
var elementArr = element.split("=");
query[elementArr[0]] = elementArr[1];
});
var varName = "name1";
console.log("by variable : " + query[varName]); // can use a variable
console.log("by string : " + query["name1"]); // can use a string
console.log("by propname : " + query.name1); // can hardcode the property name
console.log("query object:");
console.log(query);
I have a JavaScript array and I want to get the value of last name from it.
Can anyone tell how to get that from this array example:
var result = [{"FirstName":"paapu","LastName":"gandhi"}];
You have an array containing an object, so you have to retrieve the object by doing:
var myObj = result[0]
And then get the LastName property by:
var lastname = myObj.LastName
Get the first object.
var obj = result[0];
Refer to the property of the object:
var prop = result[0].FirstName;
If property name comes dynamically, that is, from a variable, use square bracket notation.
var myVar = "FirstName";
var prop = result[0][myVar];
I'd like to get an input name, as a property name, using jquery.
The html
<input name="data[First][Second]" />
The script
$.post(
'/url',
{
$("input").prop("name"): $("input").val()
}
);
how can it be done (directly)?
You can't use a variable value as a property name in a literal. You have to do it in two steps and to use the bracket notation :
var obj = {};
obj[$("input").prop("name")] = $("input").val();
$.post('/url', obj);
If you don't want to break the flow and you want an expression, you can use a function expression :
$.post(
'/url', (function(){
var obj = {};
obj[$("input").prop("name")] = $("input").val();
return obj;
})()
);
A very slight advantage is also that you don't pollute the external scope with a new variable, but it's usually not clearer.
I defined a variable which will get user's input:
var input = USER_INPUT;
then, I create an object which will use this input as an variable name inside the object:
var obj = { input: Car.newCar(...)}
Then, I try to access the obj[input], but it returns to me undefined. Is it so that in javascript, I can not use variable as an object's variable name?
If I would like to define a object which has vary variable name and variable value, how can I do?
So I guess you want the store the input under a key named after the input itself.
You can assign the value returned by Car.newCar() by using the [] method:
var input = "some text";
var obj = {};
obj[input] = Car.newCar();
Sorry changed my answer after re-reading the question
var USER_INPUT = 'something';
var obj = {};
obj[USER_INPUT] = 'value';
obj.something ; //# => value
obj['something'] ; //# => value
obj[USER_INPUT]; //# => value