Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/429.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 嵌套ifs,用于测试两个条件中的一个或两个是否为真_Javascript_Optimization_If Statement_Nested If - Fatal编程技术网

Javascript 嵌套ifs,用于测试两个条件中的一个或两个是否为真

Javascript 嵌套ifs,用于测试两个条件中的一个或两个是否为真,javascript,optimization,if-statement,nested-if,Javascript,Optimization,If Statement,Nested If,我有一个对象,data,它可能包含也可能不包含成员site\u与\u-same\u-coords和/或site\u与\u-same\u-name。我测试这些,如果其中一个或两个都存在,我会向用户发出警报: if (data.site_with_same_coords){ var SameCoordsExists = true; same_coords_message = 'The site '; same_coords_message += data.site_with_

我有一个对象,
data
,它可能包含也可能不包含成员
site\u与\u-same\u-coords
和/或
site\u与\u-same\u-name
。我测试这些,如果其中一个或两个都存在,我会向用户发出警报:

if (data.site_with_same_coords){
    var SameCoordsExists = true;
    same_coords_message = 'The site ';
    same_coords_message += data.site_with_same_coords.name;
    same_coords_message += 'already already exists in the location you have indicated';
}

if (data.site_with_same_name){
    var SameNameExists = true;
    same_name_message = 'The site ';
    same_name_message += data.site_with_same_name.name;
    same_name_message += 'already already exists in a differnt location from the one you have indicated';
}

if (SameCoordsExists && SameNameExists){
    if(data.site_with_same_name.id != data.site_with_same_coords.id){
        alert(same_coords_message + '\n' + same_name_message);
    }else if (SameCoordsExists){
        alert(same_coords_message);
    }
    }else if (SameNameExists){
        alert(same_name_message);
    }
}

有更好的方法吗?

当然,您可以将它们放在一个数组中并加入它们:

var messages = [];

if(data.site_with_same_coords) {
    messages.push('The site ' + data.site_with_same_coords.name + ' already exists in the location you have indicated');
}

if(data.site_with_same_name && !(data.site_with_same_coords && data.site_with_same_name.id === data.site_with_same_coords.id)) {
    messages.push('The site ' + data.site_with_same_name.name + ' already exists in a different location from the one you have indicated');
}

alert(messages.join('\n'));
此外,如果用户收到以下消息,是否会有点困惑:

您指定的位置中已存在某个站点
站点“其他站点”已存在于与您指定的位置不同的位置

??只是一个想法。

这个骨架:

if (A && B) {
  if (C)
    print(msgA);
  print(msgB);
} else {
  print(msgA);
}
可以这样重写:

var AB = A && B;
if ((AB && C) || !AB)
  print(msgA);
if (AB)
  print(msgB);
如您所见,
msgA
msgB
只出现一次,因此您可以在打印字符串的地方动态创建字符串。显然,在您的情况下,
A
将是
数据。具有相同\u坐标的site\u
B
将是
数据。具有相同\u名称的site\u和C将是
具有相同\u名称.id!=data.site\u与\u-same\u-coords.id

不用说,重写后的版本可读性要差得多

更新:如果你真的需要提醒(),那么你应该:

var AB = A && B;
if ((AB && C) || !AB)
  msg += msgA;
if (AB)
  msg += msgB;
if (msg)
  alert(msg);

这是有帮助的,但我只想在另一个条件为真时显示这两条消息:
data.site\u with_same\u name.id!=data.site\u与\u-same\u-coords.id
。可以将其合并到这个解决方案中吗?看起来现在它只会打印带有相同坐标的
site\u消息或两条消息,而不会只打印带有相同名称的
site\u消息。@Keyslinger:抱歉,修复了这个问题。LOL,
data
在某些地方更改为
date
,但我想这就是我现在想要的。