循环遍历包含关键字的 localStorage

发布于 2024-12-11 18:20:36 字数 256 浏览 0 评论 0原文

有没有办法根据关键字在 javascript 或 JQuery 中循环 localStorage,而不是循环遍历所有 localStorage?我知道我能做

 for (var i = 0; i < localStorage.length; i++)...

但我宁愿做类似的事情

 $.each('localStorage:contains("keyword")')...

Is there a way to loop through localStorage in javascript or JQuery based on a keyword rather than looping through all of localStorage? I know I can do

 for (var i = 0; i < localStorage.length; i++)...

But I'd rather do something like

 $.each('localStorage:contains("keyword")')...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

花心好男孩 2024-12-18 18:20:36

不能直接对通用对象使用 DOM 选择器。

但是,您可以创建一个根据传递的函数进行过滤的小函数:

Object.filter = function(obj, func) {
    var res = {};

    for(var key in obj) {
        // filter out own properties (not length) that pass the filter function
        if(obj.hasOwnProperty(key) && key !== "length" && func(key, obj[key])) {
            res[key] = obj[key];
        }
    };

    return res;
};

然后您可以执行以下操作:

var filtered = Object.filter(localStorage, function(i, v) {
                                               // Simply speaking, '~' is the
                                               // same as checking for '!== -1'
                                               return ~i.indexOf("keyword");
                                           });
$.each(filtered, function(i, v) {
    // ...
});

使用此通用函数,您可以构建其他便利函数,例如:

Object.keyContains = function(obj, contains) {
    return Object.filter(obj, function(i, v) {
        return ~i.indexOf(contains);
    });
};

然后按照您想要的方式迭代 localStorage就像这样简单:

$.each(Object.keyContains(localStorage, "keyword"), function(i, v) {
    // ...
});

It's not directly possible to use DOM selectors for generic objects.

You could, however, create a little function that filters as per a passed function:

Object.filter = function(obj, func) {
    var res = {};

    for(var key in obj) {
        // filter out own properties (not length) that pass the filter function
        if(obj.hasOwnProperty(key) && key !== "length" && func(key, obj[key])) {
            res[key] = obj[key];
        }
    };

    return res;
};

Then you can do:

var filtered = Object.filter(localStorage, function(i, v) {
                                               // Simply speaking, '~' is the
                                               // same as checking for '!== -1'
                                               return ~i.indexOf("keyword");
                                           });
$.each(filtered, function(i, v) {
    // ...
});

Using this generic function you can build additional convenience functions like:

Object.keyContains = function(obj, contains) {
    return Object.filter(obj, function(i, v) {
        return ~i.indexOf(contains);
    });
};

Then to iterate localStorage the way you want is as easy as:

$.each(Object.keyContains(localStorage, "keyword"), function(i, v) {
    // ...
});
幽蝶幻影 2024-12-18 18:20:36

JQuery 有一个 inArray() 方法,我认为这就是您正在寻找的 -

JQuery inArray

在你的情况下,你可能需要这样的东西...

var index = jQuery.inArray("keyword", localStorage);

// 从这里用 localStorage[index] 做你需要做的事情

JQuery has an inArray() method and I think that's what you're looking for -

JQuery inArray

In your case, you probably need something like this ...

var index = jQuery.inArray("keyword", localStorage);

// do what you need to with localStorage[index] from here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文