-4

I have an object

var friends = {
    bill: {
       firstName: "A",
       lastName: "C",
       number: "999",
    },
};

How can I print the values of the object friends using a for...loop?

Andy
  • 61,948
  • 13
  • 68
  • 95
hridaynehu
  • 193
  • 1
  • 4
  • 15
  • 1
    `friends.bill.firstName` etc? – Andy Dec 15 '13 at 17:11
  • yes Andy, I want to print the values – hridaynehu Dec 15 '13 at 17:14
  • 2
    how do you want the values printed? [what code have you written to try to solve your own issue](http://whathaveyoutried.com)? – zzzzBov Dec 15 '13 at 17:14
  • @hridaynehu, you need to be more specific in your question as to what you want to do. Do you want to loop over the objects in `friends`? Do you want to print their properties/values to the console or add them to a DOM element? – Andy Dec 15 '13 at 17:18
  • Yes, I want to use for..in loop to print the properties. – hridaynehu Dec 15 '13 at 17:20
  • 1
    What have you tried so far? You know what a `for...loop` is and in my first comment I showed you how to access the values. What else do you need? – Andy Dec 15 '13 at 17:22

3 Answers3

3
var friends = {
    bill:
    {
        firstName: "A",
        lastName: "C",        
        number: "999",
    },
    print: function () {
        for (var p in this.bill)
            console.log(p + ' ' + this.bill[p]);
    }
};

This code will print, is that what you need?

friends.print();
akinuri
  • 10,690
  • 10
  • 65
  • 102
Artem
  • 2,084
  • 2
  • 21
  • 29
3

I'm going to make a suggestion that might make your coding a little easier. Instead of using an object to contain a list of friends, use an array instead. An array is a list after all, and Javascript comes with some really useful array functions that can help you in the long run.

var friends = [
  {
   name: 'Bill Z',
   firstName: "Bill",
   lastName: "X",
   number: "999",
  },
  {
   name: 'Sam Y',
   firstName: "Sam",
   lastName: "Y",
   number: "999",
  }    
];

To get the list of friends you might use a couple of loops; one to loop over the list of objects, and one to loop over the properties of each object:

function listFriends() {
  for (var i = 0, l = friends.length; i < l; i++) {
    for (var p in friends[i]) {
        console.log(p + ': ' + friends[i][p]);
    }
  }
}

Or to get HTML out:

function listFriends() {
  var html = '';
  for (var i = 0, l = friends.length; i < l; i++) {
    html += '<b>' + friends[i].firstName + '</b><br/>';
    for (var p in friends[i]) {
        html += p.toUpperCase() + ': ' + friends[i][p] + '<br/>';
    }
  }
  return html;
}

You might also like to access the list of friends to find one in particular. In this example I use filter.

function getFriend(prop, value) {
  return friends.filter(function (obj) {
    return obj[prop] === value;
  });
}

console.log(getFriend('firstName', 'Sam')) // { name="Sam Y", firstName="Sam", lastName="Y"}

Fiddle.

Hopefully you'll find some of this useful.

Andy
  • 61,948
  • 13
  • 68
  • 95
1

If you'd like to print only the members of within friends.bill, you might want to use a for..in loop, for example:

for(var p in friends.bill) 
    console.log(p + ": " + friends.bill[p])

Which will print this in the console:

firstName: A VM579:3
lastName: C VM579:3
number: 999 
p.s.w.g
  • 146,324
  • 30
  • 291
  • 331