[英]Confused with object properties. How do I not get 'undefined'?
I'm trying to create an array consisting of an object for each letter in the alphabet with two properties. 我正在尝试为包含两个属性的字母表中的每个字母创建一个由对象组成的数组。 Letter and dd_array.
字母和dd_array。 Right now I'm setting
dd_array = []
, but I would like it to be the corresponding morseDic property. 现在,我正在设置
dd_array = []
,但我希望它是相应的morseDic属性。
So for instance _charObjArray[0] = {letter: 'a', dd_array: [0,1]}
因此,例如
_charObjArray[0] = {letter: 'a', dd_array: [0,1]}
Can I get some tips on how to code this best possible?? 我可以获取一些有关如何最好地编码的提示吗?
alpha_array = ["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"];
morseDic = {
a: [0,1],
b: [1,0,0,0],
c: [1,0,1,0],
d: [1,0,0],
e: [0],
f: [0,0,1,0],
g: [1,1,0],
h: [0,0,0,0],
i: [0,0],
j: [0,1,1,1],
k: [1,0,1],
l: [0,1,0,0],
m: [1,1],
n: [1,0],
o: [1,1,1],
p: [0,1,1,0],
q: [1,1,0,1],
r: [0,1,0],
s: [0,0,0],
t: [1],
u: [0,0,1],
v: [0,0,0,1],
w: [0,1,1],
x: [1,0,0,1],
y: [1,0,1,1],
z: [1,1,0,0]
}
function _alpha(_char, dd_array) {
this.letter = _char;
this.dd_array = dd_array;
}
var _charObjArray = []
for (i = 0; i < alpha_array.length; i++) {
var _charNow = alpha_array[i];
var __charNow = _charNow;
var __charNow = new _alpha(_charNow, "[]") // "[]" should be replaced with the _charNow corresponding property from morseDic
_charObjArray.push(__charNow)
}
console.log(_charObjArray);
In _alpha
, instead of: 在
_alpha
,而不是:
this.dd_array = dd_array;
...you use _char
to index into the morseDic
object: ...您使用
_char
索引到morseDic
对象:
this.dd_array = morseDic[_char];
...and then don't pass a second argument to _alpha
at all. ...然后根本不将第二个参数传递给
_alpha
。
But if you want to pass it to _alpha
as an argument: 但是,如果要将其作为参数传递给
_alpha
:
var __charNow = new _alpha(_charNow, morseDic[_charNow]);
In JavaScript, you can access object properties either with dot notation and a property name literal ( obj.foo
, or morseDic.a
), or using brackets notation and a property name string * ( obj["foo"]
or morseDic["a"]
). 在JavaScript中,您可以使用点表示法和属性名称文字(
obj.foo
或morseDic.a
),也可以使用方括号表示法和属性名称字符串 *( obj["foo"]
或morseDic["a"]
)。 In the latter case, the string can be the result of any expression, including a variable lookup. 在后一种情况下,字符串可以是任何表达式的结果,包括变量查找。
* In ES6, it'll be strings or Symbol
s. *在ES6中,它将是字符串或
Symbol
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.