jS ASCII 排序方法

        /**按ascii码从小到大排序
         *
         * @param obj
         * @returns {string}
         */
        function sort_ascii(obj) {
            let arr = new Array();
            let num = 0;
            for (let i in obj) {
                arr[num] = i;
                num++;
            }
            let sortArr = arr.sort();
            //let sortObj = {};    //完成排序值
            let str = '';             //自定义排序字符串
            for (let i in sortArr) {
                str += sortArr[i] + '=' + obj[sortArr[i]] + '&';
                //sortObj[sortArr[i]] = obj[sortArr[i]];
            }
            //去除两侧字符串
            let char = '&'
            str = str.replace(new RegExp('^\\' + char + '+|\\' + char + '+$', 'g'), '');

            return str;
            //return sortObj;
        }

        let data = {"username": "admin", "pwd": "passwd"};
        let s_data = sort_ascii(data);
        console.log(s_data);
        let sign = CryptoJS.MD5(s_data).toString();
        console.log(sign);
image.png

你可能感兴趣的:(jS ASCII 排序方法)