0

有没有人可以帮助我完成这个功能。它应该做的是在字符串中设置一个属性,该字符串首先由每个控件的冒号 (:) 分割,它检查是否有匹配的 id,如果有,然后检查是否有匹配的属性如果有一个属性覆盖值但我的函数似乎没有覆盖它只是返回原始字符串的属性。有人可以帮忙吗

var cookieValue = 'id=1&state=normal&theme=purple:id=2&state=maximized&theme=pink:id=3&state=maximized&theme=black';

var setProperties = function (cookie, id, prop, prop_value) {
    var windows = cookie.split(':');            

    var result = $.each(windows, function(index, value) {
        var temp1 = [];
        if(value.indexOf(id) > -1) {
            var temp2 = [];
            var properties = value.split('&');
            var result2 = $.each(properties, function(index, value) {

                if(value.indexOf(prop) > -1) {

                    temp3 = [];
                    temp3 = value.split('=');

                    temp3[1] = prop_value;  

                    temp2.push(temp3.join('='));
                }else {

                    temp2.push(value);
                }

                return temp2.join('&')

            });
            temp1.push(result2.join('&'));
            return temp1

        }
        else{
            temp1.push(value);  
        }
        return temp1;
    })
    return alert(result.join(':'));

}

setProperties(cookieValue, '2', 'theme', 'black');
4

2 回答 2

1

尝试:

function setProperties(cookie, id , name, value) {
    var sections = $.map(cookie.split(":"), function (section) {
        var pairs, found = false;

        if (section.indexOf("id=" + id) === 0) {
            pairs = $.map(section.split("&"), function (pair) {
                if (pair.indexOf(name + "=") === 0) {
                    found = true;

                    return name + "=" + value;
                } else {
                    return pair;
                }
            });

            if (!found) {
                pairs.push(name + "=" + value);
            }

            return pairs.join("&");
        } else {
            return section;
        }
    });

    return sections.join(":");
}
于 2013-07-06T01:19:24.610 回答
0

Each不返回值。你有一些分号丢失了我稍微编辑了代码。它不值得生产,但至少它返回(部分)正确的值。

您将必须弄清楚如何替换 cookie 中的该值。我认为正则表达式是最好的方法,或者您当然可以在函数之间传递 temp1 数组,但是您将不得不大量重构代码。

var cookieValue = 'id=1&state=normal&theme=purple:id=2&state=maximized&theme=pink:id=3&state=maximized&theme=black';

var setProperties = function (cookie, id, prop, prop_value) {
    var windows = cookie.split(':');            

    var result = $.each(windows, function(index, value) {
        var temp1 = [];
        console.log('value' + value);
        console.log('windows'  + windows);
        console.log(cookieValue);
        if(value.indexOf(id) > -1) {
            var temp2 = [];
            var properties = value.split('&');
            var windows = $.each(properties, function(index, value) {

                if(value.indexOf(prop) > -1) {

                    temp3 = [];
                    temp3 = value.split('=');

                    temp3[1] = prop_value;  

                    temp2.push(temp3.join('='));
                }else {

                    temp2.push(value);
                }

                cookieValue  = temp2.join('&');

            });
            temp1.push(temp2.join('&'));
            cookieValue = temp1;

        }
        else{
            temp1.push(value);  
        }
        cookeValue =  temp1;
    })
console.log("new cookie" + cookieValue); // PRINTS new cookieid=2&state=maximized&theme=black

}
setProperties(cookieValue, '2', 'theme', 'black');
于 2013-07-06T01:19:09.667 回答