Here we have developed one contact list. Which is an array of objects. When any user want to get the contact no. of anybody he is just to pass the value in a function findProp().
findProp(firstValue, secondValue){}
this function takes two values firstValue is the name of person of whose you want to access the data and the secondValue is the property name of object which you want to get. for example
findProp("shyam", “number”);
this will return number of shyam user in contact list.
Have a look on the code below:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Make a Contact List In JavaScript</title>
</head>
<body>
<script>
var contacts = [
{
"firstName":"Shyam",
"lastName":"Dubey",
"number":"7895498754",
"likes":["cow","buffalo"]
},
{
"firstName":"Ram",
"lastName":"Dubey",
"number":"123432124",
"likes":["cow","buffalo"]
},
{
"firstName":"Krishna",
"lastName":"Dubey",
"number":"8888888888",
"likes":["cow","buffalo"]
},
{
"firstName":"Cherry",
"lastName":"Dubey",
"number":"9999999999",
"likes":["cow","buffalo"]
},
{
"firstName":"Jatin",
"lastName":"Dubey",
"number":"1234567890",
"likes":["cow","buffalo"]
},
{
"firstName":"Gopal",
"lastName":"Sharma",
"number":"5454235323",
"likes":["cow","buffalo"],
"mobile no":"123456789"
}
];function findProp(name, prop){
for(var i =0; i<contacts.length; i++){
if(contacts[i].firstName === name){
return contacts[i][prop];
}
}
return "No such contact";}
console.log(findProp("Shyam", "number"));</script>
</body>
</html>
Output will be
7895498754