简体   繁体   English

与对象属性混淆。 我如何不变得“未定义”?

[英]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.foomorseDic.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.

相关问题 无法访问对象属性。 返回未定义(流星) - Cannot acces object properties. Returns undefined (Meteor) 原型和对象属性。 他们为什么表现不一样? - Prototypes and object properties. Why do they act differently? 我有一个带有参与者和参与者身份数组属性的对象。 这两个属性都有participantId 属性。 如何链接它们? - I have an object with participants and participantsIdentities array properties. Both properties have participantId property. How to link them? 如何根据 geoJSON 属性的最大值进行插值。 使用 Mapbox 绘制、圆形颜色、数据驱动表达式 - How do I interpolate based on max value from geoJSON properties. Using Mapbox paint, circle-color, Data driven expressions 如何构造对象的属性和属性的属性。 ES6 Javascript - How to destructure an object's property and property's properties. ES6 Javascript HTML Helpers从模型属性生成ID。 如果许多元素具有相同的ID,如何使用JavaScript和CSS定位1个特定元素? - Html Helpers generates ID from model properties. How do I target 1 particular element with JavaScript and CSS if many elements have the same ID? 如何获得函数属性的可迭代对象? - How do I get an iterable object of function properties? 如何在反应中获得 api object 属性 - How do I get api object properties in react 在带有JSON对象的jQuery中使用.each时,如何避免未定义的属性? - When using .each in jQuery with a JSON object how do I avoid undefined properties? 根据对象的属性在数组中搜索对象的问题。 (在JavaScript中) - Issue with searching an array for an object based on its properties. (In JavaScript)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM