如何转换字符串并将其作为对象调用

How to convert a string and call it as an object

我有以下带有几个元素的插件:

$.fn.greenify = function( options ) {

    // This is the easiest way to have default options.
    var settings = $.extend({
        amazon: "",
        download: "some text",
        purchase: "",
        apple: ""
    }, options );
};

通常我通过 settings.item 调用元素,例如 settings.amazon。我只想循环遍历元素并检查它们是否有价值,如果有 //do something。 我写了下面的代码:

let ds = ["download", "purchase", "amazon"]

$.each(ds, function (index, value) {
    console.log($(settings)[0] + value);
});

使用上面的代码,我想检查 settings.download 并显示它的值。但是 $(settings)[0] + value 不会被 settings.downloadsettings.amazon 替换。所以它不会把它当作一个调用。

知道如何基于 ds 数组遍历元素并显示每个元素的值吗?

这样试试

let settings;

$.fn.greenify = function( options ) {
       // This is the easiest way to have default options.
       settings = $.extend({
        amazon: "",
       download: "some text",
        purchase: "",
        apple: ""
    }, options );
};

let ds = ["download", "purchase", "amazon"];

ds.forEach(value => {
    console.log(settings[value]);
});