Jump to content

looping a specific object property


jimfog

Recommended Posts

I have 2 objects:

{id:3, title:kostas Papageorgiou, staff_ID:5, start:2014-04-09 04:30:00, end:2014-04-09 06:00:00,…}1: {id:4, title:gianis, staff_ID:5, start:2014-04-10 04:00:00, end:2014-04-10 06:00:00, color:#0072C6,…}

I want to loop only through the property title of them.How am I going to do it?

 

I have found code in the web that loops through all of the properties but not for a specific one.

In other words and having as an example the above I want to get Kostas Papageorgiou and gianis.

Link to comment
Share on other sites

This is the code I am using which and which loops through all of the properties:

    for (var key in event) {                      if (event.hasOwnProperty(key)) {                        console.dir(key + " -> " + event[key]);                      }                    }

As you see I am printing the results in the console.event is the name of the object

 

As I said above I want to access only the title property.

Link to comment
Share on other sites

I do not understand the problem. Are you unaware that you can access object properties directly? If so, how did you get this far without knowing that? You can use event.title, or event["title"]. Why are you looping through the properties?

Link to comment
Share on other sites

if(obj.property){  console.log('obj has property defined');}

That's not going to work with properties that have values such as "", 0 or false

Link to comment
Share on other sites

I think it's odd that I can't use...

var t = events[1].1.title; // 'yyy'

for...

var events = [{0:{id:1,title:'xxx',start:100}},{1:{id:2,title:'yyy',start:150}}];
Link to comment
Share on other sites

That's not going to work with properties that have values such as "", 0 or false

Ah yes, correct. the property will be there, but will not pass that test. I guess I oversimplified the example of object notation.

Link to comment
Share on other sites

I do not understand the problem. Are you unaware that you can access object properties directly? If so, how did you get this far without knowing that? You can use event.title, or event["title"]. Why are you looping through the properties?

Because there is more than one object at any moment. That is why I am using loop.

Anyway I found a workaround and no loop is needed at all...but I am just letting you know why I used loop in the first place.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...