简体   繁体   English

在块 [] 中使用 if-else 语句

[英]Using if-else statement in a block []

I have the following statement:我有以下声明:

arr.push([student.name,student.id]);

I want to add an if-else block in this statement so I could add student.name only if the if is true, something like:我想在这个语句中添加一个if-else块,这样我就可以只在if为真时添加student.name ,例如:

arr.push([student.name if (validName(student.name))
         ,student.id]);

But this syntax is not valid.但是这种语法是无效的。 Is there a proper way to achieve this idea?有没有合适的方法来实现这个想法?

EDIT :编辑

My array contains a lot of elements and I want to add to most of them (if not all of them) some kind of check.我的数组包含很多元素,我想向其中的大多数(如果不是全部)添加某种检查。 Something like:就像是:

    arr.push([student.name if (validName(student.name)),
              student.id   if (validId(student.id) && idContainsNine(student.id)),
              student.avg  if (student.avg !== 'undefined'),
              ...
              ]);

I push the values only if the if statement is true.只有当if语句为真时,我才会推送这些值。 If it false, it will not push it.如果它是假的,它不会推动它。 for example if validName(student.name) returns false it will not push student.name in the array.例如,如果validName(student.name)返回 false,它不会将student.name推送到数组中。

You could take a conditional operator and get only an array with valid values.您可以使用条件运算符并仅获取具有有效值的数组。

array.push(validName(student.name)
    ? [student.name, student.id]
    : [student.id]
);

An other approach is to use functions which return after a check either an array with the value or without.另一种方法是使用在检查后返回有值或没有值的数组的函数。

For using this result, you need to spread the array and if it does not contain any element, it does not increase the length of the array.为了使用这个结果,你需要扩展数组,如果它不包含任何元素,它不会增加数组的长度。

cost 
    includeName = name => validName(name) ? [name] : [],
    includeId = id => validId(id) && idContainsNine(id) ? [id] : [],
    includeAVG = avg => avg !== 'undefined' ? [avg] : [];

arr.push([
    ...includeName(student.name),
    ...includeId(student.id),
    ...includeAVG(student.avg)
]);

Another solution could be to use an array of checks and return in case of unwanted items an own Symbol (if undefined or ``null` is a used value) and filter the array before pushing it to the result set.另一种解决方案可能是使用检查数组并在不需要的项目的情况下返回一个自己的Symbol (如果undefined或“null”是一个使用的值)并在将数组推送到结果集之前过滤数组。

const
    EMPTY = new Symbol('empty'),
    checkName = ({ name }) => validName(name) ? name : EMPTY,
    checkId = ({ id }) => validId(id) && idContainsNine(id) ? id : EMPTY,
    checkAVG = ({ avg }) => avg !== 'undefined' ? avg : EMPTY;


array.push([checkName, checkId, checkAVG]
    .map(fn => fn(student))
    .filter(v => v !== EMPTY)
);

Consider ternary operator.考虑三元运算符。

arr.push([validName(student.name) ? student.name : '', student.id]);

Replace empty string value '' with what you want when you get invalid student.name .当您获得无效的student.name时,将空字符串值''替换为您想要的值。

Use this one.It may help you.使用这个。它可以帮助你。

arr.push([student.name,(student.id ? student.id : '')]);

you can use && operator, second statement will be executed only if the first one is true.您可以使用&&运算符,只有当第一个为真时才会执行第二个语句。

  validName(student.name) && arr.push([student.name,student.id]);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM