Javascript object value returning undefined with string key

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

Javascript object value returning undefined with string key

问题

Sure, here's the translated code:

  1. 所以我试图根据其他元素的属性来更新元素的类我能够正确获取所需的值例如
  2. alignment = "start"
  3. 但是当我尝试从对象中获取其关联的值时它返回undefined可能的原因是什么此外是否有更好的方法来实现这一目标
  4. // 更新对齐按钮
  5. var alignmentMap = {
  6. "end": "bi-text-right",
  7. "start": "bi-text-left",
  8. "center": "bi-text-center",
  9. "justify": "bi-justify"
  10. }
  11. var alignment = $(activeId + " .component-content").css("text-align");
  12. console.log(alignmentMap[alignment]);
  13. alignment = alignmentMap[alignment];
  14. $("#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.

  1. //Update Alignment Button
  2. var alignmentMap = {
  3. "end": "bi-text-right",
  4. "start": "bi-text-left",
  5. "center": "bi-text-center",
  6. "justify": "bi-justify"
  7. }
  8. var alignment = $(activeId+" .component-content").css("text-align");
  9. console.log(alignmentMap[alignment]);
  10. alignment = alignmentMap[alignment];
  11. $("#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.

  1. //Update Alignment Button
  2. var alignmentMap = {
  3. "right": "bi-text-right",
  4. "left": "bi-text-left",
  5. "center": "bi-text-center",
  6. "justify": "bi-justify"
  7. }
  8. var alignment = $(activeId+" .component-content").css("text-align");
  9. console.log(alignmentMap[alignment]);
  10. alignment = alignmentMap[alignment];
  11. $("#cp-align .settings-container i").attr("class", "bi "+alignment);
  12. </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:

确定