JS 实现Json查询的方法实例(2)

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);
                    fn = _cache[exp] = _complite(code);
                }
                return fn(this);
            } catch (e) {
                return [];
            }
        }

var doTest = function () {

var heros = [
            // 名============攻=====防=======力量====敏捷=====智力====
                {name: '冰室女巫', DP: 38, AP: 1.3, Str: 16, Dex: 16, Int: 21 },
                { name: '沉默术士', DP: 39, AP: 1.1, Str: 17, Dex: 16, Int: 21 },
                { name: '娜迦海妖', DP: 51, AP: 6.0, Str: 21, Dex: 21, Int: 18 },
                { name: '赏金猎人', DP: 39, AP: 4.0, Str: 17, Dex: 21, Int: 16 },
                { name: '剧毒术士', DP: 45, AP: 3.1, Str: 18, Dex: 22, Int: 15 },
                { name: '光之守卫', DP: 38, AP: 1.1, Str: 16, Dex: 15, Int: 22 },
                { name: '炼金术士', DP: 49, AP: 0.6, Str: 25, Dex: 11, Int: 25 }
            //...
            ];

var match = heros.Query('@Str>20 AND @Dex>20');
            ShowResult(match[0]);

            // 查询:“士”结尾的
            // 结果:沉默术士,剧毒术士,炼金术士
            var match = heros.Query('right(@name,1)="士"');
            ShowResult(match[0]);
        }

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wdgsws.html