Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Defining variable and converting JSON data
- var userData = JSON.stringify([
- {
- "id" : 101,
- "firstName" : "Rohit",
- "lastName" : "Sharma",
- "email": "srrexhsharma@gmail.com",
- "phone" : 9999477068,
- "address" : { "houseNo": "G-145 A", "street": "Street No. 14", "colony" : "Kunwar Singh Nagar", "landmark" : "Hanuman Mandir, Nangloi", "City" : "Delhi", "State" : "New Delhi", "PIN" : 110041},
- "alternateNo" : 7503070277
- },
- {
- "id" : 102,
- "firstName" : "Dheeraj",
- "lastName" : "Sharma",
- "email": "perfectdheerajsharma@gmail.com",
- "phone" : 7503070277,
- "address" : { "houseNo": "G-145 A", "street": "Street No. 14", "colony" : "Kunwar Singh Nagar", "landmark" : "Hanuman Mandir, Nangloi", "City" : "Delhi", "State" : "New Delhi", "PIN" : 110041},
- "alternateNo" : 9999477068
- },
- {
- "id" : 103,
- "firstName" : "Salman",
- "lastName" : "Malik",
- "email": "salmanmaliksahab@gmail.com",
- "phone" : 9899618647,
- "address" : { "houseNo": "45 B", "street": "Street No. 4", "colony" : "Nanngloi No. 2", "landmark" : "Metro Station, Nangloi", "City" : "Delhi", "State" : "New Delhi", "PIN" : 110041},
- "alternateNo" : 7503070277
- },
- {
- "id" : 104,
- "firstName" : "Nitish",
- "lastName" : "Kumar",
- "email": "ennitish@gmail.com",
- "phone" : 9654774788,
- "address" : { "houseNo": "B-87", "street": "Street No. 6", "colony" : "Shiv Ram Park", "landmark" : "Hanuman Mandir, Nangloi", "City" : "Delhi", "State" : "New Delhi", "PIN" : 110041},
- "alternateNo" : 7503070277
- }
- ]);
- // Function to read data from JSON stored variable
- function readData()
- {
- var userDb = JSON.parse(userData);
- console.log(userDb);
- }
- // calling readData function
- readData();
- // Function to find particular entry from JSON store using name or id
- function findEntryById(id){
- var data = JSON.parse(userData);
- console.log(data.filter(function(person){
- return person.id == id;
- }));
- }
- // calling findEntryByName function
- findEntryById();
- // Function to find a particular entry by its position/index
- function findEntryByPosition(position){
- var dataNew = JSON.parse(userData);
- console.log(dataNew[position]);
- }
- // calling findEntryByPosition function
- findEntryByPosition();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement