Normal output


#1

How can I code the contact list excercise so that it looks normal and not like an object? like this:
First Name: Steve
Last Name: Jobs
Number: (408) 555-5555
Address: 1 Infinite Loop
Cupertino, CA 95014

I tried this:
console.log(friends[key], friends[key].firstName);
but it didnt work. as you can see it logs the whole object again...I don't know what to type instead of friends[key] to get only the property name and not the whole object


Search for a friend help
#2

can you post your full code?

This exercise also calls the function to validate your code works correctly, this also causes output


#3

Thanks for the quick response..

var friends = {};

friends.steve = {
    firstName: "Steve",
    lastName: "Jobs",
    number: "(089) 333-4444",
    address: ['Apple', 'Hollywood', 'California', 'CA', '98550']
    };
friends.bill = {
    firstName: "Bill",
    lastName: "Gates",
    number: "(206) 555-5555",
    address: ['One Microsoft Way','Redmond','WA','98052']
    };
var list = function(friends) {
  for (var key in friends) {
      console.log(key);
      
  }
};



var search = function(name){
   for (var key in friends)  {
       if (friends[key].firstName === name) {
           

           console.log(friends[key], friends[key].firstName);
           return (friends[key].firstName);
       }
       
       else {
           console.log("no such name!");
       }
      
   }
}
search("Steve");

#4

i ran your code in a bin, it all seems fine?

you have 3 outputs:

console.log(friends[key], friends[key].firstName);
return (friends[key].firstName);

this code matches what you see in the output


#5

yeah the output is an object plus the first name ..
I'd like to have the output not as an object. it should look like this with no curely braces :
First Name: Steve
Last Name: Jobs
Number: (408) 555-5555
Address: 1 Infinite Loop
Cupertino, CA 95014

I think it should be something like this :
console.log(XXXXXX, ": ", friends[key].firstName)
console.log(XXXXXX ": ", friends[key].lastName)
console.log(XXXXXX, ": ", friends[key].number)
console.log(XXXXXX,": ", friends[key].address)

instead of those XXXXX I should type something in to get only the property .
and for address even more...


#6

this is not very DRY, if you want to do this, i recommend another for in loop


#7

you r right..it d be nicer with a for loop, wouldnt have to repeat 5 times or more. I'd appreciate it if you told me how would it look like


#8

Wouldn't be great to try first? It could you teach you so much

Here is a hint:
friends contains your friends object, you used a for in loop to loop over friends. In which case key (the iterator) contained the friends properties and friends[key] contained the friends values.
friends[key] contains your steve object. That should help


#9

Thanks for the help. it works. it turned out to be like this:

var friends = {};

friends.steve = {
    firstName: "Steve",
    lastName: "Jobs",
    number: "(089) 333-4444",
    address: ['Apple', 'Hollywood', 'California', 'CA', '98550']
    };
friends.bill = {
    firstName: "Bill",
    lastName: "Gates",
    number: "(206) 555-5555",
    address: ['One Microsoft Way','Redmond','WA','98052']
    };
var list = function(friends) {
  for (var key in friends) {
      console.log(key);
      
  }
};




var search = function(name){
   for (var key in friends)  {
       if (friends[key].firstName === name) {
            for (i in friends[key])
                console.log(i + ": " + friends[key][i])
                return (i + ": " + friends[key][i]);

           
       }
       
       else {
           console.log("no such name!");
       }
      
   }
}
search("Steve");

#10

that looks really good? Are you happy with it?


#11

yeah..i wanted to learn how to log properties and propertiy values.
though i couldnt still figure out how to log propertiy without using for loop.
for instance :
var myObject = {firstName :"John", lastName: "Doe"}
in order to have only firstName and lastName to be logged to the console, what should I code?


#12

btw. do you have any better solution than the above code?


#13

your code is good, if you just want the first and lastname, simply use one console.log:

console.log(firstName + "\n" + lastName)

firstName and lastName are just for demonstration purpose


#14

Ok. Thanks for the help


#15