際際滷

際際滷Share a Scribd company logo
Search in JSON Arrays
Pankaj Bhageria
Friday 23 August 13
Traditional way
Friday 23 August 13
With Where JS
Friday 23 August 13
where([1,1,1,2],1); // outputs [1,1,1]
where(['aa','bb','aa','ab','bc'],'aa'); //['aa','aa']
//Numbers
where([1,1,3,4],[1,2,4]); // [1,1,4]
//or
where([1,1,3,4],{$in:[1,2,4]}) // [1,1,4]
//Strings
where(['aa','bb','aa','ab','bc'],['aa','ab']); //['aa',,'aa','ab']
Simple Search
Friday 23 August 13
var users = [
{name:{fname:'David',lname:'Jones'},age:28, salary:10000 },
{name:{fname:'James',lname:'Smit'},age:38, salary: 20000 },
{name:{fname:'Paul',lname:'Lee'},age:19,salary:60000 },
{name:{fname:'David',lname:'Lee'},age:29,salary:40000 },
]
where(users,{name:{fname:'David'}});
//OR
where(users,{'name.fname':'David'});
Deep Search
Friday 23 August 13
where([3,4,5,6,7],{$lt:5}) // returns [3,4]
where(users,{age:{$gt:30} });
where(users,{age:{$lt:20}, salary:{$gt:50000} });
Number Operations
Friday 23 August 13
var input = ["Agra", "Amritsar", "Bangalore"];
where(input, {$startsWith:'A'} );//["Agra","Amritsar"];
input = [{x:'aaAaa'},{x:"bbAaabcd"},{x:'Baabcd'}];
where(input, {x:{$contains:'Aaa'}} ); //[{x:'aaAaa'},{x:"bbAaabcd"}];
String Operations
Friday 23 August 13
where(users,{$or:[ {age:{$gt:30}},{salary:{$gt:50000}}] });
where(users,{$not:{fname:'David'}});
Logical Operations
Friday 23 August 13
where([3,4,5,6,7],function(o){ return (o%2 == 0) } ) // [4,6]
where(users,{age:function(a){ return a == 50} } )
Condition as a Function
Friday 23 August 13
select js
A JavaScript library to map and transform json
object/arrays
Friday 23 August 13
var input = {x:1,y:2,z:3};
var output = select(input,{rename:{x:'a',y:'b',z:'c'}}); //output is {a:1,b:
2,c:3}
Renaming Properties
Friday 23 August 13
var input = {x:1,y:2,z:3};
var output = select(input,{only:["y","z"]}); // output is {y:2,z:3}
var input = {x:1,y:2,z:3};
var output = select(input,{only:["y"],rename:{y:"b"} }); //output is {b:2}
Only
Friday 23 August 13
var input = {x:1,y:2,z:3};
var output = select(input,{except:["y"]});//output is {x:1,z:3}
var input = {x:1,y:2,z:3};
var output = select(input,{except:["y"],rename:{x:"a",z:"c"} });//output is
{b:1,c:3}
Except
Friday 23 August 13
var input = {x:1,y:2,z:3};
var output = select(input,{only:[],
methods:{sum:function(input){
var sum = input.x + input.y + input.z;
var avg = sum/3.0;
return {sum:sum,avg:avg}
}
}
});
//output is {sum:6,avg:2}
Methods
Friday 23 August 13
var input = {x:1,y:2,z:3};
var output = select(input,{only:[],
methods:{sum:function(input){
var sum = input.x + input.y + input.z;
var avg = sum/3.0;
return {sum:sum,avg:avg}
}
}
});
//output is {sum:6,avg:2}
Arrays
Friday 23 August 13
The End
Friday 23 August 13

More Related Content

Search in JSON arrays using where and select js

  • 1. Search in JSON Arrays Pankaj Bhageria Friday 23 August 13
  • 3. With Where JS Friday 23 August 13
  • 4. where([1,1,1,2],1); // outputs [1,1,1] where(['aa','bb','aa','ab','bc'],'aa'); //['aa','aa'] //Numbers where([1,1,3,4],[1,2,4]); // [1,1,4] //or where([1,1,3,4],{$in:[1,2,4]}) // [1,1,4] //Strings where(['aa','bb','aa','ab','bc'],['aa','ab']); //['aa',,'aa','ab'] Simple Search Friday 23 August 13
  • 5. var users = [ {name:{fname:'David',lname:'Jones'},age:28, salary:10000 }, {name:{fname:'James',lname:'Smit'},age:38, salary: 20000 }, {name:{fname:'Paul',lname:'Lee'},age:19,salary:60000 }, {name:{fname:'David',lname:'Lee'},age:29,salary:40000 }, ] where(users,{name:{fname:'David'}}); //OR where(users,{'name.fname':'David'}); Deep Search Friday 23 August 13
  • 6. where([3,4,5,6,7],{$lt:5}) // returns [3,4] where(users,{age:{$gt:30} }); where(users,{age:{$lt:20}, salary:{$gt:50000} }); Number Operations Friday 23 August 13
  • 7. var input = ["Agra", "Amritsar", "Bangalore"]; where(input, {$startsWith:'A'} );//["Agra","Amritsar"]; input = [{x:'aaAaa'},{x:"bbAaabcd"},{x:'Baabcd'}]; where(input, {x:{$contains:'Aaa'}} ); //[{x:'aaAaa'},{x:"bbAaabcd"}]; String Operations Friday 23 August 13
  • 9. where([3,4,5,6,7],function(o){ return (o%2 == 0) } ) // [4,6] where(users,{age:function(a){ return a == 50} } ) Condition as a Function Friday 23 August 13
  • 10. select js A JavaScript library to map and transform json object/arrays Friday 23 August 13
  • 11. var input = {x:1,y:2,z:3}; var output = select(input,{rename:{x:'a',y:'b',z:'c'}}); //output is {a:1,b: 2,c:3} Renaming Properties Friday 23 August 13
  • 12. var input = {x:1,y:2,z:3}; var output = select(input,{only:["y","z"]}); // output is {y:2,z:3} var input = {x:1,y:2,z:3}; var output = select(input,{only:["y"],rename:{y:"b"} }); //output is {b:2} Only Friday 23 August 13
  • 13. var input = {x:1,y:2,z:3}; var output = select(input,{except:["y"]});//output is {x:1,z:3} var input = {x:1,y:2,z:3}; var output = select(input,{except:["y"],rename:{x:"a",z:"c"} });//output is {b:1,c:3} Except Friday 23 August 13
  • 14. var input = {x:1,y:2,z:3}; var output = select(input,{only:[], methods:{sum:function(input){ var sum = input.x + input.y + input.z; var avg = sum/3.0; return {sum:sum,avg:avg} } } }); //output is {sum:6,avg:2} Methods Friday 23 August 13
  • 15. var input = {x:1,y:2,z:3}; var output = select(input,{only:[], methods:{sum:function(input){ var sum = input.x + input.y + input.z; var avg = sum/3.0; return {sum:sum,avg:avg} } } }); //output is {sum:6,avg:2} Arrays Friday 23 August 13
  • 16. The End Friday 23 August 13