admin管理员组文章数量:1130349
I have a problem while adding values to a JavaScript object: the value to add is a key,value pair. Here is sample:
//JavaScript object
var cart=new Object();
function add()
{
var rating="1"
var category="somecat";
var user="user1";
if(cart[user]==null)
cart[user]={category:rating};
else
cart[user][category]=rating;
}
What I was expecting is that if user exists in cart object then value for his particular should get replaced, and if user doesn't exist then new user and category should be added.
The code is working fine when user already exists. Problem is, when I am adding a new element with cart[user]={category:rating} then its adding the variable name as key i.e. category , not the value inside it ("somecat").
Is there any way to do this using json, jquery or javascript itself?
Is there any way to assign value inside the variable?
I have a problem while adding values to a JavaScript object: the value to add is a key,value pair. Here is sample:
//JavaScript object
var cart=new Object();
function add()
{
var rating="1"
var category="somecat";
var user="user1";
if(cart[user]==null)
cart[user]={category:rating};
else
cart[user][category]=rating;
}
What I was expecting is that if user exists in cart object then value for his particular should get replaced, and if user doesn't exist then new user and category should be added.
The code is working fine when user already exists. Problem is, when I am adding a new element with cart[user]={category:rating} then its adding the variable name as key i.e. category , not the value inside it ("somecat").
Is there any way to do this using json, jquery or javascript itself?
Is there any way to assign value inside the variable?
Share Improve this question edited Sep 12, 2012 at 10:54 mornaner 2,4242 gold badges28 silver badges39 bronze badges asked Sep 12, 2012 at 10:42 Nikhil RupanawarNikhil Rupanawar 4,21111 gold badges36 silver badges51 bronze badges 03 Answers
Reset to default 7There is no way to use a variable to define a property name inside object literal notation. It accepts identifiers or strings, but both identify property names, not variables.
You have to create the object then add the property:
if(!cart[user]) {
cart[user] = {};
}
cart[user][category] = rating;
You will need to replace
{category:rating}
with
var obj = {};
obj[category] = rating;
When creating an object literal the key must be a string, so in your code it will be "category". If instead you do:
var rating="1"
var category="somecat";
var user="user1";
if( !cart[user] ) {
cart[user]={};
}
cart[user][category]=rating;
That will initialise a non existing user to an empty object and then add the category.
I have a problem while adding values to a JavaScript object: the value to add is a key,value pair. Here is sample:
//JavaScript object
var cart=new Object();
function add()
{
var rating="1"
var category="somecat";
var user="user1";
if(cart[user]==null)
cart[user]={category:rating};
else
cart[user][category]=rating;
}
What I was expecting is that if user exists in cart object then value for his particular should get replaced, and if user doesn't exist then new user and category should be added.
The code is working fine when user already exists. Problem is, when I am adding a new element with cart[user]={category:rating} then its adding the variable name as key i.e. category , not the value inside it ("somecat").
Is there any way to do this using json, jquery or javascript itself?
Is there any way to assign value inside the variable?
I have a problem while adding values to a JavaScript object: the value to add is a key,value pair. Here is sample:
//JavaScript object
var cart=new Object();
function add()
{
var rating="1"
var category="somecat";
var user="user1";
if(cart[user]==null)
cart[user]={category:rating};
else
cart[user][category]=rating;
}
What I was expecting is that if user exists in cart object then value for his particular should get replaced, and if user doesn't exist then new user and category should be added.
The code is working fine when user already exists. Problem is, when I am adding a new element with cart[user]={category:rating} then its adding the variable name as key i.e. category , not the value inside it ("somecat").
Is there any way to do this using json, jquery or javascript itself?
Is there any way to assign value inside the variable?
Share Improve this question edited Sep 12, 2012 at 10:54 mornaner 2,4242 gold badges28 silver badges39 bronze badges asked Sep 12, 2012 at 10:42 Nikhil RupanawarNikhil Rupanawar 4,21111 gold badges36 silver badges51 bronze badges 03 Answers
Reset to default 7There is no way to use a variable to define a property name inside object literal notation. It accepts identifiers or strings, but both identify property names, not variables.
You have to create the object then add the property:
if(!cart[user]) {
cart[user] = {};
}
cart[user][category] = rating;
You will need to replace
{category:rating}
with
var obj = {};
obj[category] = rating;
When creating an object literal the key must be a string, so in your code it will be "category". If instead you do:
var rating="1"
var category="somecat";
var user="user1";
if( !cart[user] ) {
cart[user]={};
}
cart[user][category]=rating;
That will initialise a non existing user to an empty object and then add the category.
本文标签: jqueryVariable name getting added instead of its valueJavaScriptStack Overflow
版权声明:本文标题:jquery - Variable name getting added instead of its value, javascript - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://it.en369.cn/questions/1744916508a2123932.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。


发表评论