代码
function where ( fn ) {
var $json = [ ] ;
this . map ( function ( val, index ) {
if ( fn) {
var t = fn ( val, index) ;
if ( t == true ) {
$json. push ( val) ;
}
}
} ) ;
return $json;
}
function Find ( fn ) {
var $json = false ;
for ( var i = 0 ; i < this . length; i++ ) {
if ( fn) {
var t = fn ( this [ i] ) ;
if ( t == true ) {
$json= this [ i] ;
break ;
}
}
}
return $json;
}
function FindKey ( key, val ) {
var $json = false ;
for ( var i = 0 ; i < this . length; i++ ) {
if ( this [ i] [ key] == val) {
$json= this [ i] ;
break ;
}
}
return $json;
}
function select ( fn ) {
var $json = [ ] ;
this . map ( function ( val, index ) {
if ( fn) {
var t = fn ( val, index) ;
if ( t) {
$json. push ( t) ;
}
}
}
) ;
return $json;
}
function GroupBy ( n ) {
var list = [ ] ;
var _t= this ;
for ( var i = 0 ; i < this . length; i++ ) {
var json_str = '{"' + n + '":"' + this [ i] [ n] + '"}' ;
if ( list. length > 0 ) {
var l = list. where2 ( JSON . parse ( json_str) ) ;
if ( l. length == 0 ) {
list. push ( JSON . parse ( json_str) )
}
} else {
list. push ( JSON . parse ( json_str) )
}
}
for ( var i = 0 ; i < list. length; i++ ) {
list[ i] . list = this . where ( function ( val ) {
if ( val[ n] == list[ i] [ n] ) {
return true ;
}
return false
} ) ;
}
return list;
}
function OrderBy ( _sort, by ) {
by= by|| 'asc'
this . sort ( function ( a, b ) {
if ( typeof ( a[ _sort] ) == 'number' || ( ! isNaN ( a[ _sort] ) ) ) {
if ( by== 'asc' ) {
return Number ( a[ _sort] ) - Number ( b[ _sort] ) ;
}
if ( by== 'desc' ) {
return Number ( b[ _sort] ) - Number ( a[ _sort] ) ;
}
}
if ( typeof ( a[ _sort] ) == 'string' ) {
if ( by== 'asc' ) {
return a[ _sort] . localeCompare ( b[ _sort] ) ;
}
if ( by== 'desc' ) {
return b[ _sort] . localeCompare ( a[ _sort] ) ;
}
}
} ) ;
return this ;
}
function ToArray ( _clo ) {
if ( ! _clo) {
console. error ( '没有指定字段!' ) ;
return [ ] ;
}
var arr= [ ] ;
this . map ( function ( val, i ) {
arr. push ( val[ _clo] ) ;
} ) ;
return arr;
}
function limit ( pageindex, pagesize ) {
return this . skip ( ( pageindex- 1 ) * pagesize) . take ( pagesize) ;
}
function take ( n ) {
var list= [ ] ;
if ( n> this . length) {
n= n- ( n- this . length) ;
}
for ( var i = 0 ; i < n; i++ ) {
list. push ( this [ i] ) ;
}
return list;
}
function skip ( n ) {
var list= [ ] ;
if ( n> this . length) {
n= this . length;
}
for ( var i= n; i< this . length; i++ ) {
list. push ( this [ i] ) ;
}
return list;
}
function sum ( col, spl= ',' ) {
var count= '' ;
this . map ( function ( val ) {
if ( typeof ( val[ col] ) == 'number' ) {
count= Number ( count) ;
count+= Number ( val[ col] ) ;
} else {
count+= val[ col] + spl;
}
} ) ;
return count;
}
Array . prototype. where= where;
Array . prototype. select= select;
Array . prototype. GroupBy= GroupBy;
Array . prototype. OrderBy= OrderBy;
Array . prototype. ToArray= ToArray;
Array . prototype. Find= Find;
Array . prototype. FindKey= FindKey;
Array . prototype. limit= limit;
Array . prototype. take= take;
Array . prototype. skip= skip;
Array . prototype. sum= sum;
function Contains ( str ) {
if ( this . indexOf ( str) > - 1 ) {
return true ;
}
return false ;
}
String . prototype. Contains= Contains;
使用方法
json数据
var json= [
{ "name" : "Q蕠蕠" , "sex" : "女" , "age" : 24 } ,
{ "name" : "Q援" , "sex" : "男" , "age" : 46 } ,
{ "name" : "Q陹雭" , "sex" : "男" , "age" : 35 } ,
{ "name" : "Q黬" , "sex" : "女" , "age" : 41 } ,
{ "name" : "Q畺" , "sex" : "女" , "age" : 26 } ,
{ "name" : "Q湻" , "sex" : "女" , "age" : 51 } ,
{ "name" : "Q銃" , "sex" : "男" , "age" : 37 } ,
{ "name" : "Q沺" , "sex" : "男" , "age" : 12 } ,
{ "name" : "Q弈" , "sex" : "男" , "age" : 28 } ,
{ "name" : "Q鱣鱣" , "sex" : "女" , "age" : 31 } ,
{ "name" : "Q卼" , "sex" : "男" , "age" : 3 } ,
{ "name" : "Q皽廘" , "sex" : "女" , "age" : 21 } ,
{ "name" : "Q锱閸" , "sex" : "男" , "age" : 40 } ,
{ "name" : "Q败败" , "sex" : "男" , "age" : 56 } ,
{ "name" : "Q幇換" , "sex" : "男" , "age" : 40 } ,
{ "name" : "Q罢" , "sex" : "女" , "age" : 28 } ,
{ "name" : "Q癧黉" , "sex" : "女" , "age" : 63 } ,
{ "name" : "Q孖俒" , "sex" : "女" , "age" : 26 } ,
{ "name" : "Q耤耤" , "sex" : "女" , "age" : 5 } ,
{ "name" : "Q猢" , "sex" : "女" , "age" : 31 }
]
where 筛选数据
var new_json= json. where ( function ( val ) {
return val. age> 20 ;
} ) ;
console. log ( new_json)
Find/FindKey 筛选数据 返回一条
var new_json = json. Find ( function ( val ) {
return val. name== 'Q蕠蕠' ;
} ) ;
console. log ( new_json)
var new_json = json. FindKey ( 'name' , 'Q蕠蕠' ) ;
console. log ( new_json)
select 重组数据(仿C# select)
var new_json = json. select ( function ( val ) {
return {
a : val. name,
b : val. age
}
} ) ;
console. log ( new_json)
GroupBy 根据指定字段分组
var new_json = json. GroupBy ( "sex" ) ;
console. log ( new_json)
OrderBy 根据指定字段排序
var new_json = json. OrderBy ( "age" ) ;
console. log ( new_json)
ToArray 将指定字段的值转化为数组
var new_json = json. ToArray ( "age" ) ;
console. log ( new_json)
limit 分页
var new_json = json. limit ( 1 , 5 ) ;
console. log ( new_json)
sum 将指定列相加
var new_json = json. sum ( 'age' ) ;
console. log ( new_json)