Javascript object value returning undefined with string key

huangapple go评论60阅读模式
英文:

Javascript object value returning undefined with string key

问题

Sure, here's the translated code:

所以我试图根据其他元素的属性来更新元素的类我能够正确获取所需的值例如

alignment = "start"

但是当我尝试从对象中获取其关联的值时它返回undefined可能的原因是什么此外是否有更好的方法来实现这一目标

// 更新对齐按钮
var alignmentMap = {
    "end": "bi-text-right",
    "start": "bi-text-left",
    "center": "bi-text-center",
    "justify": "bi-justify"
}
var alignment = $(activeId + " .component-content").css("text-align");
console.log(alignmentMap[alignment]);
alignment = alignmentMap[alignment];
$("#cp-align .settings-container i").attr("class", "bi " + alignment);

I've translated the code part and removed the unnecessary instructions.

英文:

So, I'm trying to update the class of an element based on the properties of some other element. I am able to correctly get the required value, for instance

alignment = "start"

But when I try to get its associated value from the object, it returns undefined, what might be the possible reasons for that. Also ,is there a better way to achieve this.

//Update Alignment Button
    var alignmentMap = {
        "end": "bi-text-right",
        "start": "bi-text-left",
        "center": "bi-text-center",
        "justify": "bi-justify"
    }
    var alignment = $(activeId+" .component-content").css("text-align");
    console.log(alignmentMap[alignment]);
    alignment = alignmentMap[alignment];
    $("#cp-align .settings-container i").attr("class", "bi "+alignment);

答案1

得分: 0

以下是代码中需要翻译的部分:

  • Available text-align property in CSS are left, right, center, justify. 如果 text-align CSS 属性设置为 text-align: left,那么 alignment 的值应为 left,并且 alignmentMap 对象中相应的键应为 left,而不是 start。对于 right 也是一样的。

  • var alignmentMap = {
    "right": "bi-text-right",
    "left": "bi-text-left",
    "center": "bi-text-center",
    "justify": "bi-justify"
    }

  • var alignment = $(activeId+ " .component-content").css("text-align");

  • console.log(alignmentMap[alignment]);

  • alignment = alignmentMap[alignment];

  • $("#cp-align .settings-container i").attr("class", "bi " +alignment);

英文:

Available text-align property in CSS are left, right, center, justify. If the text-align CSS property is set to text-alight: left, then the alignment value should be left and corresponding key in the alignmentMap object should be left, not start. Same for right as well.

//Update Alignment Button
var alignmentMap = {
    "right": "bi-text-right",
    "left": "bi-text-left",
    "center": "bi-text-center",
    "justify": "bi-justify"
}
var alignment = $(activeId+" .component-content").css("text-align");
console.log(alignmentMap[alignment]);
alignment = alignmentMap[alignment];
$("#cp-align .settings-container i").attr("class", "bi "+alignment);

</details>



huangapple
  • 本文由 发表于 2023年5月7日 21:35:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/76194255.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定