如何使用JavaScript以单词开头的所有cookie?

use*_*630 -2 javascript cookies

我如何获得一个包含所有以开头的cookie名称的数组word

Sco*_*ott 5

完全实用的方法:

document.cookie.split(';').filter(function(c) {
    return c.trim().indexOf('word') === 0;
}).map(function(c) {
    return c.trim();
});
Run Code Online (Sandbox Code Playgroud)

附带说明:

//Get a list of all cookies as a semicolon+space-separated string
document.cookie.split(';')
//Filter determines if an element should remain in the array.  Here we check if a search string appears at the beginning of the string
.filter(function(c) {
    return c.trim().indexOf('word') === 0;
})
//Map applies a modifier to all elements in an array, here we trim spaces on both sides of the string
.map(function(c) {
    return c.trim();
});
Run Code Online (Sandbox Code Playgroud)

ES6:

document.cookie.split(';')
    .filter(c => c.startsWith('word'));
Run Code Online (Sandbox Code Playgroud)