如何通过参数获取Chrome本地存储结果的值映射

How to get the value mapping of a Chrome local storage result through an argument?

本文关键字:结果 存储 映射 何通过 参数 获取 Chrome      更新时间:2023-09-26

我有一个函数:

            //  Get the value out of Chrome local storage
        chrome.storage.local.get(sourcePath, function(result) {
                //  Test the result
            alert(JSON.stringify(result.sourcePath));
            });

还有一个检查电话:

chrome.storage.local.set({'userAcceptanceAgreement': true});

现在,我想知道如何让我的函数在使用上述参数调用时,将该值传递到result.sourcePath警报中。照原样,它显示的result具有正确的存储值{'userAcceptanceAgreement': true},但result.sourcePath显示为未定义,因为它不是试图为参数定位键:值对,而是为文本sourcePath定位。

此处为基本JavaScript。

var sourcePath = 'userAcceptanceAgreement';
result.sourcePath; // Accesses literally "sourcePath"
result[sourcePath]; // Accesses "userAcceptanceAgreement"

请注意,您在大量噪音中掩盖了问题。请考虑下次构造一个最小的示例