var bob = { firstName: "Bob", lastName: "Jones", phoneNumber: "(650) 777-7777", email: "bob.jones@example.com" }; var mary = { firstName: "Mary", lastName: "Johnson", phoneNumber: "(650) 888-8888", email: "mary.johnson@example.com" }; var contacts = [bob, mary]; function printPerson(person) { console.log(person.firstName + " " + person.lastName); } function list() { var contactsLength = contacts.length; for (var i = 0; i < contactsLength; i++) { printPerson(contacts[i]); } } /*Create a search function then call it passing "Jones"*/ function search(lastName){ var contactsLength=contacts.length; for(var n=0;n<contactsLength;n++){ if(lastName===contacts[n].lastName) { printPerson(contacts[n]); } }} search("Jones") function add(firstName,lastName,email,phoneNumber) { //var contacts=new Object(); contacts.firstName=firstName; contacts.lastName=lastName; contacts.email=email; contacts.phoneNumber=phoneNumber; contacts[contacts.length]=contacts; } add("tinga","tina","123@126.com","12346") list()
一、在语言方面,一直都是以一种好奇的心态去玩玩,从没认真去专研过某一种语言,有时候想想,真是一种悲哀;
在过关斩将的时候,有时会思考下,这样的方法,在我们实战做系统中,是否也应这样去操作?
a. 显示数组中的数据;
function list() {
var contactsLength = contacts.length;
for (var i = 0; i < contactsLength; i++) {
printPerson(contacts[i]);
}
}
b.搜索,在实现一个搜索功能的时候,如果有匹配的就显示出来;
function search(lastName){
var contactsLength=contacts.length;
for(var n=0;n<contactsLength;n++){
if(lastName===contacts[n].lastName)
{
printPerson(contacts[n]);
}
}}
c.增加一个对象;
function add(firstName,lastName,email,phoneNumber)
{
//var contacts=new Object();
contacts.firstName=firstName;
contacts.lastName=lastName;
contacts.email=email;
contacts.phoneNumber=phoneNumber;
contacts[contacts.length]=contacts;
}