[英]Conditionally set an object property
Is there some syntax for setting properties based on a condition?是否有一些基于条件设置属性的语法?
data: {
userId: 7,
actionId: 36,
express: (myCondition ? true : null) // does not work
}
I want express
to be either set to a value or not set at all (ie, there should be no key named express
), and without extra statements after the definition.我希望
express
要么设置为一个值,要么根本不设置(即,应该没有名为express
键),并且在定义之后没有额外的语句。 I know I can use it as a boolean, but the receiving side is using an isset()
check and I'm wondering if I can avoid modifying it.我知道我可以将它用作布尔值,但接收方正在使用
isset()
检查,我想知道是否可以避免修改它。
Edit : Seems there is no direct solution to the problem as stated.编辑:似乎没有直接解决上述问题的方法。 Here are the close suggestions:
以下是接近的建议:
JSON.stringify (Chris Kessel, dystroy): JSON.stringify (Chris Kessel,dystroy):
var json = JSON.stringify( {
data: {
userId: 7,
actionId: 36,
express: (myCondition ? true : null)
}
});
An anonymous function (Paulpro):匿名函数(Paulpro):
var data = new function(){
this.userId = 7;
this.actionId = 36;
myCondition && (this.express = true);
};
An extra statement (x4rf41):额外的语句(x4rf41):
data: {
userId: 7,
actionId: 36
}
if(myCondition) data["express"] = true;
Eval (a former colleague of mine): Eval (我的前同事):
eval("data = {userId: 7, actionId: 36 " + (myCondition ? ", express: true}" : "}"))
Conditional definition (don't really know how to label this one):条件定义(真的不知道如何标记这个):
data = (
(myCondition && { userId: 7, actionId: 36, express: true }) ||
(!myCondition && { userId: 7, actionId: 36 })
);
Use the spread operator .使用扩展运算符。
data: {
userId: 7,
actionId: 36,
...myCondition && {express: true}
}
Note that if you're using Flow , that syntax might generate type check errors.请注意,如果您使用Flow ,则该语法可能会生成类型检查错误。 You can write the above more explicitly, and less succinctly, as:
您可以更明确、更简洁地编写上述内容,如下所示:
data: {
userId: 7,
actionId: 36,
...(myCondition ? {express: true} : {})
}
Do it like this :像这样做 :
data: {
userId: 7,
actionId: 36,
express: (myCondition ? true : undefined)
}
A property whose value is undefined
isn't written when you stringify the object to JSON.将对象字符串化为JSON 时,不会写入值
undefined
属性。
EDIT : It appears from the comments that there is no JSON involved in fact.编辑:从评论看来,实际上没有涉及 JSON。 OP is using
$.ajax
so $.param
is probably used. OP 正在使用
$.ajax
因此可能使用了$.param
。 $.param
, unfortunately, does create an entry for properties whose value is undefined
.不幸的是,
$.param
确实为值为undefined
属性创建了一个条目。 So there's probably no solution without any supplementary line of code.因此,如果没有任何补充代码行,可能就没有解决方案。
You can do it if you define your object using an anonymous function instead of object literal notation:如果您使用匿名函数而不是对象文字表示法定义对象,则可以这样做:
var data = new function(){
this.userId = 7;
this.actionId = 36;
myCondition && (this.express = true);
};
The resulting data
object is the exact same, except it's constructor
will be the anonymous function instead of window.Object
.结果
data
对象完全相同,除了它的constructor
将是匿名函数而不是window.Object
。
You could do something like this:你可以这样做:
var json = JSON.stringify( {
data: {
userId: 7,
actionId: 36,
express: (myCondition ? true : null)
}
});
A bit old but there is a good solution as well you can do :有点旧,但也有一个很好的解决方案,您可以这样做:
data: {
userId: 7,
actionId: 36
}
Object.assign(data, !myCondition && { express: yourValue });
Thus it will assign your express property with the value you need if your condition is false.因此,如果您的条件为假,它将为您的 express 属性分配您需要的值。
first of all, thats javascript, not JSON.首先,那是 javascript,而不是 JSON。
the solution:解决方案:
data: {
userId: 7,
actionId: 36
}
if(myCondition) data["express"] = true;
The spread operator now fixes this.扩展运算符现在修复了这个问题。 Here is an example with two comparisons.
这是一个有两个比较的例子。
Note: I changed date:
to const date =
so that it is valid runnable javascript.注意:我将
date:
更改为const date =
以便它是有效的可运行 javascript。 This can also be used with data:
if it is supposed to be inside of a deep object structure.这也可以用于
data:
如果它应该在深层对象结构内部。
const compareValue = 13; const data = { userId: 7, actionId: 36, ...(compareValue > 10 && {propertyForGreaterThan10: 'foo'}), ...(compareValue < 10 && {propertyForLessThan10: 'bar'}), } console.log(data);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.