Home  >  Article  >  Web Front-end  >  JS method example to implement Json query_javascript skills

JS method example to implement Json query_javascript skills

WBOY
WBOYOriginal
2016-05-16 17:37:161023browse

It’s actually very simple. In this part of my code, the first part is a simple implementation of how to use JS to write templates, and the second part is a specific implementation of an extension of JSON query.

There will be a powerful tool for querying Json in the future.

Copy code The code is as follows:

/*
         * 定义模板函数
        */
        var template = function (queryArr) {

            var count = 0;

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

var e = queryArr[i];

if ($express) {
count++;
}
}

return count;
}

/*
* 模板创建函数
*/
var createIntance = function (exp) {
var fun = template.toString().replace("$express", exp).toString();
return eval("0," + fun);
}

var testTodo = function () {

var testArr = [
{ name: "张三", age: 20 },
{ name: "李四", age: 25 },
{ name: "王二麻子", age: 28 },
{ name: "小张", age: 30 }
];

var func = createIntance("e.age>=25");

            alert(func(testArr));
        }

        /****************** JS 实现 JSON查询 **********************/

        // 定义常用的函数
        var len = function (s) { return s.length; }
        var left = function (s, n) { return s.substr(0, n); }
        var right = function (s, n) { return s.substr(-n); }
        var index = function (s, find) { return s.indexOf(find) + 1; }

        // 扩展原型方法
        var _proto = Object.prototype;

        // 缓存,解决快速查找
        var _cache = {};

        // 扩展运算符
        var _alias = [
            /@/g, "_e.",
            /AND/gi, "&&",
            /OR/gi, "||",
            /<>/g, "!=",
            /NOT/gi, "!",
            /([^=<>])=([^=]|$)/g, '$1==$2'
        ];

        var _rQuote = /""/g;
        var _rQuoteTemp = /!~/g;

        // 编译
        var _complite = function (code) {
            return eval("0," + code);
        }

        // 将扩展符号转换成标准的JS符号
        var _interpret = function (exp) {
            exp = exp.replace(_rQuote,"!~");
            var arr = exp.split('"');
            var i, n = arr.length;
            var k = _alias.length;

            for (var i = 0; i < n; i += 2) {
var s = arr[i];
for (var j = 0; j < k; j += 2) {
if (index(s, _alias[j]) > -1) {
                        s = s.replace(_alias[j], _alias[j + 1]);
                    }
                }
                arr[i] = s;
            }

            for (var i = 1; i < n; i += 2) {
arr[i] = arr[i].replace(_rQuoteTemp, '\\"');
}
return arr.join('"');
}

// 定义模函数
var _templ = function (_list) {
var _ret = [];
var _i = -1;

for (var _k in _list) {
var _e = _list[_k];
if (_e != _proto[_k]) {
if ($C) {
_ret[++_i] = _e;
}
}
}
return _ret;
} .toString();

// 扩展查询的方法
_proto.Query = function (exp) {
if (!exp) {
return [];
}

var fn = _cache[exp];

try {
if (!fn) {
var code = _interpret(exp);
code = _templ.replace("$C ", code);
                                                                        ] = _complite (code); }
}

var doTest = function () {

var heos = [
// name ============ Attack ========== Power ======= Intelligence ==== <> {name: 'ice room witch', DP: 38, AP: 1.3, STR: 16, DEX: 16, int: 21},

{name: 'silent warlock', DP : 39, AP: 1.1, Str: 17, Dex: 16, Int: 21 },

                                                                                                                               : 18 },

{ name: 'Bounty Hunter', DP: 39, AP: 4.0, Str: 17, Dex: 21, Int: 16 },

: 45, AP: 3.1, Str: 18, Dex: 22, Int: 15 },
                                                                                                                                                         22 },
                                                                                                                                                                                                                                                             
var match = heroes.Query('@Str>20 AND @Dex>20');
ShowResult(match[0]);

// Query:                                                                                                                                                                                  ;
}

function ShowResult(result) {
} alert(result.name " " result.DP " " result.AP " " result.Str " " result.Dex " " result.Int);

}



This is the code. Everyone is welcome to make suggestions or come up with new ideas. Let’s expand together

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn