【问题标题】:How can I get a specific value out of a list of values?如何从值列表中获取特定值?
【发布时间】:2021-06-27 19:37:25
【问题描述】:

我不确定如何从声明的列表中选择特定值。我猜答案与我正在尝试的相差不远。我做错了什么?

function specialSlider(vali, prop) {
    const selected = [
        {prop_1: "normal", prop_2: "multiply", prop_3: "screen", prop_4: "overlay", prop_5: "darken", prop_6: "lighten", prop_7: "color-dodge", prop_8: "saturation", prop_9: "color", prop_10: "luminosity",},
        {prop_1: "circle", prop_2: "triangle", prop_3: "rhombus", prop_4: "square", prop_5: "stripes", prop_6: "frame", prop_7: "star", prop_8: "diamond", prop_9: "pear", prop_10: "custom",}
        ];
    if (vali == 1) {
        target.style.setProperty("--blend", selected[prop][prop_1]);
    } else if (vali == 2) {
        target.style.setProperty("--blend", selected[prop][prop_2]);
    } else if (vali == 3) {
        target.style.setProperty("--blend", selected[prop][prop_3]);
    } else if (vali == 4) {
        target.style.setProperty("--blend", selected[prop][prop_4]);
    } else if (vali == 5) {
        target.style.setProperty("--blend", selected[prop][prop_5]);
    } else if (vali == 6) {
        target.style.setProperty("--blend", selected[prop][prop_6]);
    } else if (vali == 7) {
        target.style.setProperty("--blend", selected[prop][prop_7]);
    } else if (vali == 8) {
        target.style.setProperty("--blend", selected[prop][prop_8]);
    } else if (vali == 9) {
        target.style.setProperty("--blend", selected[prop][prop_9]);
    } else if (vali == 10)  {
        target.style.setProperty("--blend", selected[prop][prop_10]);
    } else {
        console.log('error')
    }
}

【问题讨论】:

    标签: javascript profile


    【解决方案1】:

    如果您使用括号表示法来获取属性值,则必须提供带有属性名称的字符串。例如,在下面的代码摘录中,您引用了一个名为“prop_10”的变量,即undefined

    selected[prop][prop_10]
    

    因此,正确的应该是:

    selected[prop]['prop_10']
    

    【讨论】:

      猜你喜欢
      • 2016-09-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多