Javascript method for Json query _ javascript skills

Source: Internet
Author: User
I have read a template class written by Daniel to implement Json. Today I am trying to write it myself. Fortunately, I still remember some things and thought it was clear. Directly Writing the code is actually very simple. The first part of my code is simple implementation of how to use JS to write templates, and the second is to implement an extension of JSON query.

Json query will be available in the future.

The Code is as follows:


/*
* Define template Functions
*/
Var template = function (queryArr ){

Var count = 0;

For (var I = 0; I <queryArr. length; I ++ ){

Var e = queryArr [I];

If ($ express ){
Count ++;
}
}

Return count;
}

/*
* Template creation Function
*/
Var createIntance = function (exp ){
Var fun = template. toString (). replace ("$ express", exp). toString ();
Return eval ("0," + fun );
}

Var testTodo = function (){

Var testArr = [
{Name: "Zhang San", age: 20 },
{Name: "Li Si", age: 25 },
{Name: "Wang Erma zi", age: 28 },
{Name: "Xiao Zhang", age: 30}
];

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

Alert (func (testArr ));
}

/***************** JSON query in JS ***************** *****/

// Define common functions
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 ;}

// Extended Prototype Method
Var _ proto = Object. prototype;

// Cache for quick search
Var _ cache = {};

// Extension operator
Var _ alias = [
/@/G, "_ e .",
/AND/gi ,"&&",
/OR/gi, "| ",
/<>/G ,"! = ",
/NOT/gi ,"! ",
/([^ = <>]) = ([^ =] | $)/G, '$1 = $2'
];

Var _ rQuote =/""/g;
Var _ rQuoteTemp = /!~ /G;

// Compile
Var _ complite = function (code ){
Return eval ("0," + code );
}

// Convert the extension symbol into a standard JS symbol
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 ('"');
}

// Define a model function
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 ();

// Extended Query Method
_ 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 = intelligence ====
{Name: 'ice witch ', DP: 38, AP: 1.3, Str: 16, Dex: 16, Int: 21 },
{Name: 'silent locker', DP: 39, AP: 1.1, Str: 17, Dex: 16, Int: 21 },
{Name: 'naga haidao ', DP: 51, AP: 6.0, Str: 21, Dex: 21, Int: 18 },
{Name: 'bounty hunter ', DP: 39, AP: 4.0, Str: 17, Dex: 21, Int: 16 },
{Name: 'highly toxic wareer', DP: 45, AP: 3.1, Str: 18, Dex: 22, Int: 15 },
{Name: 'guard of light ', DP: 38, AP: 1.1, Str: 16, Dex: 15, Int: 22 },
{Name: 'authorization', DP: 49, AP: 0.6, Str: 25, Dex: 11, Int: 25}
//...
];

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

// Query:
// Result: silent locks, highly toxic locks, and alchemy locks
Var match = heros. Query ('Right (@ name, 1) = "Shi "');
ShowResult (match [0]);
}

Function ShowResult (result ){
Alert (result. name + "" + result. DP + "" + result. AP + "" + result. str + "" + result. dex + "" + result. int );
}


The code is like this. You are welcome to come up with new ideas. Let's expand together
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.