Advertisement
Guest User

Untitled

a guest
Oct 13th, 2017
460
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.35 KB | None | 0 0
  1. // Defining variable and converting JSON data
  2.  
  3. var userData = JSON.stringify([
  4. {
  5. "id" : 101,
  6. "firstName" : "Rohit",
  7. "lastName" : "Sharma",
  8. "email": "srrexhsharma@gmail.com",
  9. "phone" : 9999477068,
  10. "address" : { "houseNo": "G-145 A", "street": "Street No. 14", "colony" : "Kunwar Singh Nagar", "landmark" : "Hanuman Mandir, Nangloi", "City" : "Delhi", "State" : "New Delhi", "PIN" : 110041},
  11. "alternateNo" : 7503070277
  12.  
  13. },
  14. {
  15. "id" : 102,
  16. "firstName" : "Dheeraj",
  17. "lastName" : "Sharma",
  18. "email": "perfectdheerajsharma@gmail.com",
  19. "phone" : 7503070277,
  20. "address" : { "houseNo": "G-145 A", "street": "Street No. 14", "colony" : "Kunwar Singh Nagar", "landmark" : "Hanuman Mandir, Nangloi", "City" : "Delhi", "State" : "New Delhi", "PIN" : 110041},
  21. "alternateNo" : 9999477068
  22.  
  23. },
  24. {
  25. "id" : 103,
  26. "firstName" : "Salman",
  27. "lastName" : "Malik",
  28. "email": "salmanmaliksahab@gmail.com",
  29. "phone" : 9899618647,
  30. "address" : { "houseNo": "45 B", "street": "Street No. 4", "colony" : "Nanngloi No. 2", "landmark" : "Metro Station, Nangloi", "City" : "Delhi", "State" : "New Delhi", "PIN" : 110041},
  31. "alternateNo" : 7503070277
  32.  
  33. },
  34. {
  35. "id" : 104,
  36. "firstName" : "Nitish",
  37. "lastName" : "Kumar",
  38. "email": "ennitish@gmail.com",
  39. "phone" : 9654774788,
  40. "address" : { "houseNo": "B-87", "street": "Street No. 6", "colony" : "Shiv Ram Park", "landmark" : "Hanuman Mandir, Nangloi", "City" : "Delhi", "State" : "New Delhi", "PIN" : 110041},
  41. "alternateNo" : 7503070277
  42.  
  43. }
  44. ]);
  45.  
  46. // Function to read data from JSON stored variable
  47.  
  48. function readData()
  49. {
  50. var userDb = JSON.parse(userData);
  51. console.log(userDb);
  52. }
  53.  
  54. // calling readData function
  55. readData();
  56.  
  57. // Function to find particular entry from JSON store using name or id
  58.  
  59. function findEntryById(id){
  60. var data = JSON.parse(userData);
  61. console.log(data.filter(function(person){
  62. return person.id == id;
  63. }));
  64. }
  65.  
  66. // calling findEntryByName function
  67. findEntryById();
  68.  
  69. // Function to find a particular entry by its position/index
  70.  
  71. function findEntryByPosition(position){
  72. var dataNew = JSON.parse(userData);
  73. console.log(dataNew[position]);
  74. }
  75.  
  76. // calling findEntryByPosition function
  77. findEntryByPosition();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement