get name of json
JSON
product82127600211="a"
product82127600212="b"
product82127600213="c"
javascript
var idCompany=8212760021;
var idProduct="produc开发者_开发技巧t"+idCompany+"1";
alert(products.idProduct); // this line show undefined
this alert show undefined but if use
alert(products.product82127600211);
show a
success.
now how can create idProduct that show a
Your syntax is wrong, use brackets instead:
alert(products[idProduct]);
Currently you are trying to access a variable named idProduct
, i.e. your example would be equivalent to:
alert(products["idProduct"]);
alert(products[idProduct]); // this line show a
Would
alert(products[idProduct]);
not work?
try
alert(products[idProduct]);
To use a variable key for an JavaScript object, use the bracket notation (["key"]
) instead of the dot notation (.key
). In your example you would:
alert(products[idProduct])
I believe your problem is that the reference to idProduct is not attached to your object products.
e.g.
products = {};
products.idProduct="product"+idCompany+"1";
alert(products.idProduct);
The alert shows the value of idProduct
精彩评论