我很难在javascript中使用if else条件在对象中创建新键。这是一个对象:
userInfo: {
userid: userInfo.userid,
pid: userInfo.pid,
usernameType: userInfo.usernameType,
if(getCookieValue('JetPrivilege_CCounter_C', 'userAcceptedCookie')=="true"){
cookieAccept:'Y'
cookieDateTime: getCookieValue('JetPrivilege_CCounter_C', 'ad')
sessionID : JSESSIONID
cookieAcceptedProperty : getCookieValue('JetPrivilege_CCounter_C', 'as');
sessionDateTime :JSESSIONTIME;
}
else
{
cookieAccept:'N'
},
activeStatus: userInfo.activeStatus,
age: userInfo.age,
gender: userInfo.gender
}
我需要的是如果我的条件为真,那么创建对象
userInfo: {
userid: userInfo.userid,
pid: userInfo.pid,
usernameType: userInfo.usernameType,
cookieAccept:'Y'
cookieDateTime: getCookieValue('JetPrivilege_CCounter_C', 'ad')
sessionID : JSESSIONID
cookieAcceptedProperty : getCookieValue('JetPrivilege_CCounter_C', 'as');
sessionDateTime :JSESSIONTIME;
activeStatus: userInfo.activeStatus,
age: userInfo.age,
gender: userInfo.gender
}
否则
userInfo: {
userid: userInfo.userid,
pid: userInfo.pid,
usernameType: userInfo.usernameType,
cookieAccept:'N'
activeStatus: userInfo.activeStatus,
age: userInfo.age,
gender: userInfo.gender
}
答案 0 :(得分:1)
您不能将if else
语句直接放入对象声明中。您可以做的是使用静态值创建对象,并使用if else
在声明后为对象赋值。 :
userInfo: {
userid: userInfo.userid,
pid: userInfo.pid,
usernameType: userInfo.usernameType,
activeStatus: userInfo.activeStatus,
age: userInfo.age,
gender: userInfo.gender
}
if (getCookieValue('JetPrivilege_CCounter_C', 'userAcceptedCookie') == "true") {
userInfo.cookieAccept = 'Y'
userInfo.cookieDateTime = getCookieValue('JetPrivilege_CCounter_C', 'ad')
userInfo.sessionID = JSESSIONID
userInfo.cookieAcceptedProperty = getCookieValue('JetPrivilege_CCounter_C', 'as');
userInfo.sessionDateTime = JSESSIONTIME;
} else {
userInfo.cookieAccept = 'N'
}
答案 1 :(得分:0)
在对象后面添加密钥:
var data = {
userInfo: {
userid: userInfo.userid,
pid: userInfo.pid,
usernameType: userInfo.usernameType,
activeStatus: userInfo.activeStatus,
age: userInfo.age,
gender: userInfo.gender
};
if (getCookieValue('JetPrivilege_CCounter_C','userAcceptedCookie')=="true") {
data.userInfo.cookieAccept:'Y';
data.userInfo.ookieDateTime: getCookieValue('JetPrivilege_CCounter_C', 'ad');
data.userInfo.sessionID : JSESSIONID;
data.userInfo.cookieAcceptedProperty : getCookieValue('JetPrivilege_CCounter_C', 'as');
data.userInfo.sessionDateTime :JSESSIONTIME;
} else {
data.userInfo.cookieAccept:'N';
}
答案 2 :(得分:0)
另一种方法如下:
let userInfo = {
userid: userInfo.userid,
pid: userInfo.pid,
usernameType: userInfo.usernameType,
...getCookieValue('JetPrivilege_CCounter_C', 'userAcceptedCookie') && {
cookieAccept: 'Y',
cookieDateTime: getCookieValue('JetPrivilege_CCounter_C', 'ad'),
sessionID: JSESSIONID,
cookieAcceptedProperty: getCookieValue('JetPrivilege_CCounter_C', 'as'),
sessionDateTime: JSESSIONTIME
},
...!getCookieValue('JetPrivilege_CCounter_C', 'userAcceptedCookie') && {
cookieAccept: 'N'
},
activeStatus: userInfo.activeStatus,
age: userInfo.age,
gender: userInfo.gender
}
这是因为如果对象文字的扩展运算符的操作数是一个没有可枚举的自有属性的对象,那么它就不会做任何事情[See]
您可以在此处测试简化版本:
let userInfo = {
userid: 1,
...false && {
cookieAccept: 'Y',
cookieDateTime: 1,
sessionID: 1,
cookieAcceptedProperty: 1,
sessionDateTime: 1
},
...true && {
cookieAccept: 'N'
},
activeStatus: 2,
age: 2,
gender: 2
}
console.log(userInfo)