In my node (express) app, I want to send a json response back to the client. It would look something like this.
在我的节点(快速)应用程序中,我想将json响应发送回客户端。它看起来像这样。
{"someTshirt":
{small : 'available'},
{med : 'available'},
{large : 'not available'}
}
I'd reiterate through the sizes and append to the response set with its availability. How would I create this object to begin with in plain javascript within app.js? how would I add the 'someTshirtName' to the beginning of this object as well as appending each size's availability to it after the object has been created?
我将重申这些大小并附加响应集及其可用性。如何在app.js中的普通javascript中创建此对象?如何将“someTshirtName”添加到此对象的开头,并在创建对象后将每个大小的可用性附加到其中?
2 个解决方案
#1
7
You can build your object like this:
您可以像这样构建对象:
var availability = {"someTshirt":
{
'small': 'available',
'med' : 'available',
'large' : 'not available'
}
};
Then you can access this object with:
然后您可以使用以下方法访问此对象
availability.someTshirt.small
>>> 'available'
availability.someTshirt.large
>>> 'not available'
However I'd recommend you to use booleans instead of strings, which are easier to manipulate. You can still change the display string later:
但是我建议你使用布尔代替字符串,这些字符串更易于操作。您仍然可以在以后更改显示字符串:
var availability = {"someTshirt":
{
'small': true,
'med' : true,
'large' : false
}
};
if (availability.someTshirt.small) {
console.log('available');
}
>>> 'available'
[edit] Response to the comment:
[编辑]回应评论:
If you want to create your objects dynamically, you can do the following:
如果要动态创建对象,可以执行以下操作:
var availability = {};
availability.someTshirt = {};
availability.someTshirt.small = true;
availability.someTshirt.med = true;
availability.someTshirt.large = false;
if (availability.someTshirt.small) {
console.log("available");
} else {
console.log("not available");
}
>>> 'available'
availability.someTshirt.small = false;
if (availability.someTshirt.small) {
console.log("available");
} else {
console.log("not available");
}
>>> 'not available'
#2
1
If you need to build your object based on JSON string returned to your code, you can use eval
statement. for example you have a string variable sJsonResult
containing your JSON response. Your code can go something like;
如果需要根据返回给代码的JSON字符串构建对象,可以使用eval语句。例如,您有一个包含JSON响应的字符串变量sJsonResult。你的代码可以像这样;
var sJsonResult = "{someTshirt: {small : 'available', med : 'available',large : 'not available'}}";
var o;
eval("o = " + sJsonResult);
alert(o.someTshirt.small);
#1
7
You can build your object like this:
您可以像这样构建对象:
var availability = {"someTshirt":
{
'small': 'available',
'med' : 'available',
'large' : 'not available'
}
};
Then you can access this object with:
然后您可以使用以下方法访问此对象
availability.someTshirt.small
>>> 'available'
availability.someTshirt.large
>>> 'not available'
However I'd recommend you to use booleans instead of strings, which are easier to manipulate. You can still change the display string later:
但是我建议你使用布尔代替字符串,这些字符串更易于操作。您仍然可以在以后更改显示字符串:
var availability = {"someTshirt":
{
'small': true,
'med' : true,
'large' : false
}
};
if (availability.someTshirt.small) {
console.log('available');
}
>>> 'available'
[edit] Response to the comment:
[编辑]回应评论:
If you want to create your objects dynamically, you can do the following:
如果要动态创建对象,可以执行以下操作:
var availability = {};
availability.someTshirt = {};
availability.someTshirt.small = true;
availability.someTshirt.med = true;
availability.someTshirt.large = false;
if (availability.someTshirt.small) {
console.log("available");
} else {
console.log("not available");
}
>>> 'available'
availability.someTshirt.small = false;
if (availability.someTshirt.small) {
console.log("available");
} else {
console.log("not available");
}
>>> 'not available'
#2
1
If you need to build your object based on JSON string returned to your code, you can use eval
statement. for example you have a string variable sJsonResult
containing your JSON response. Your code can go something like;
如果需要根据返回给代码的JSON字符串构建对象,可以使用eval语句。例如,您有一个包含JSON响应的字符串变量sJsonResult。你的代码可以像这样;
var sJsonResult = "{someTshirt: {small : 'available', med : 'available',large : 'not available'}}";
var o;
eval("o = " + sJsonResult);
alert(o.someTshirt.small);