如果有人可以向我解释一下,我错过了一些东西。我正在尝试将现有代码重写为三元运算符的方式。我收到以下控制台错误:
Uncaught SyntaxError: Unexpected token }
据我所知,有一个格式不正确的条件,我似乎找不到。所以我不确定我错过了什么,或者我是否误解了过滤功能中的某些内容?不是吗?item.verified === true 不应该自动返回 true 的对象吗?
var engagement = "social";
var audience = [{ 'verified': true, 'name': 'Steve'},
{ 'verified': false, 'name': 'Phil'},
{ 'verified': true, 'name': 'Jason'}];
let data = [];
data = audience.filter((item) => {
(engagement === "social")
? item.verified === true
: (engagement === 'social-crm')
? item.verified === false
: (engagement === 'all')
? item
})
Run Code Online (Sandbox Code Playgroud)
我理解的语法:
data = audience.filter((item) => {
if (this.engagement === 'social-crm') {
return item.verified === true; …Run Code Online (Sandbox Code Playgroud) 我想使用三元运算符来确定变量是否应该更改。
代码如下:
var c = "hello";
var appendWorld = false;
c = appendWorld ? string.Concat(c, " world") : c;
Run Code Online (Sandbox Code Playgroud)
由于 case appendWorld为false的值与之前相同,我想知道是否有更简洁的方法来编写此代码。
请记住,这只是一个简化的示例。
我希望能够编写这样的内容:如果appendWorld为falsec = appendWorld ? string.Concat(c, " world"); ,则c自动保持不变。
这样的事情存在吗?
这是我的代码:
std::cout << "The contaner is " << (!container)?"not":; "empty";
Run Code Online (Sandbox Code Playgroud)
这显然不起作用,但我希望现在这个想法很清楚。我想打印"The container is empty",并"not"在"empty"if bool containeris之前添加false。
我想知道是否有可能,或者我是否必须按照以下方式写一些东西:
if(container) std::cout ...;
else std::cout ...;
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用三元运算符在 C++ 中编写一个简单的阶乘函数。它给出了编译错误。
int factorial(int n) {
(n == 0) ? return 1 : return n * factorial(n-1);
}
Run Code Online (Sandbox Code Playgroud)
它说Expected ':' to match this '?'
我是 C++ 的新手。
我有包含以下键和值的字典。我正在尝试打印字典,但在有空值的地方没有打印任何内容。如何在输出中打印“null”?
Dictionary<string, object> dic1 = new Dictionary<string, object>();
dic1.Add("id", 1);
dic1.Add("name", "john");
dic1.Add("grade", null);
Console.WriteLine(string.Join(Environment.NewLine, dic1.Select(a => $"{a.Key}: {a.Value}")));
Run Code Online (Sandbox Code Playgroud)
这是我得到的输出:
id: 1
name: john
grade:
Run Code Online (Sandbox Code Playgroud) 是否可以让?:操作员处理以下示例中的多个语句?
condition ? FirstTrueExpression SecondTrueExpression : FirstFalseExpression SecondFalseExpression
Run Code Online (Sandbox Code Playgroud)
有没有办法避免指定“else”语句?
condition ? TrueExpression
Run Code Online (Sandbox Code Playgroud) var a = '1';
console.log(a == ('2'||'1')?'hi':'hello');
Run Code Online (Sandbox Code Playgroud)
这样做条件失败为a = 1。由于此条件失败,它将 a 的值 1 与 2 进行比较。所以它总是打印你好。有没有办法检查“||”之后的值('1')所以它打印嗨?
拒绝C规则的动机是什么a conditional expression does not yield an lvalue?
换句话说:在 C 中,它是not yield an lvalue. 为什么在 C++ 中这种动机被拒绝(或重新考虑)?
假设我们有以下表达式:
(x>y) ? printf("type of int") : func_string()
那么我们这里有什么?
x>y -> true -> printf("type of int") -> the type of the expression is int (because printf function is an int type function).
或者
x>y -> false -> calls func_string() function which for the purpose of the question, returns a string.
结论是,对于这个表达式的结果,我们只有 2 个选项:一个int function (prinf)OR 一个字符串function (func_string)。
这意味着表达式有 2 种可能的类型。
然而,编译器不能真正等到运行时才能获取表达式的类型,编译器不能真正编译代码,同时不确定表达式的类型是 int 还是 string,所以我们会得到一个如果我们尝试运行这种代码,则编译错误,条件运算符的 2 个结果有 2 种不同的类型。
#include<stdio.h>
int main()
{
int a=0, b=1, c=3;
*((a) ?&b :&a) = a ? b : c;
printf("%d %d %d\n", a, b, c);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
输出:3 1 3
赋值运算符的结合性是从右到左。所以这里我们得到了 3 1 3 但是......
#include<stdio.h>
int main()
{
int a=0, b=1, c=3;
*((a++) ?&b :&a) = a ? b : c;
printf("%d %d %d\n", a, b, c);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
输出:1 1 3
My doubt is why 1 1 3 is returned instead of 1 3 3 ?
Run Code Online (Sandbox Code Playgroud) c postfix-mta variable-assignment conditional-operator postfix-operator
c ×3
c++ ×3
c# ×2
javascript ×2
cout ×1
dictionary ×1
function ×1
logical-or ×1
lvalue ×1
null ×1
optimization ×1
postfix-mta ×1
recursion ×1
types ×1