[英]Strange String Behavior in Canvas/JS
I am trying to draw connections between station nodes in Canvas. 我正在尝试在Canvas中的工作站节点之间绘制连接。 If I stick to the same pattern for station names ('A', 'B' etc.) there is no problem: https://jsfiddle.net/3k4L2ky0/ 如果我为电台名称坚持相同的模式(“ A”,“ B”等),则没有问题: https : //jsfiddle.net/3k4L2ky0/
But if I use both single-letter strings with multi-letter ones (eg 'A', 'B', 'C', 'Airport', 'Central'), Javascript seems to be treating the first letter of the longer strings as equivalent to the shorter ones: eg 'Airport' == 'A': https://jsfiddle.net/w6n0p04w/ 但是,如果我同时使用单字母字符串和多字母字符串(例如“ A”,“ B”,“ C”,“机场”,“中央”),则Javascript似乎会将较长字符串的首字母视为等同于较短的那些:例如'Airport'=='A': https : //jsfiddle.net/w6n0p04w/
Clearly the problem is in the function here: 显然问题出在这里的函数中:
function lookUp(sta) {
for (n=0; n < station.length; n++){
if (sta == station[n][0]) {
return n;
break;
}
}
}
What should I change? 我应该改变什么? Aren't 'A' and 'Airport' both distinct strings? “ A”和“机场”不是两个截然不同的字符串吗?
The problem isn't is lookUp
. 问题不是lookUp
。
The problem is you are passing the first character to lookUp
here: 问题是您将第一个字符传递给此处的lookUp
:
var stat = lookUp(startStat[0]);
. 。
Simply change that to the following and it will work as expected. 只需将其更改为以下内容即可正常运行。
var stat = lookUp(startStat);
See: https://jsfiddle.net/w6n0p04w/2/ 参见: https : //jsfiddle.net/w6n0p04w/2/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.