jimfog Posted April 24, 2014 Share Posted April 24, 2014 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 More sharing options...
justsomeguy Posted April 24, 2014 Share Posted April 24, 2014 Just loop through the array of objects and access the title property of each one. I'm not sure where you're stuck with that. What code do you have? Link to comment Share on other sites More sharing options...
jimfog Posted April 24, 2014 Author Share Posted April 24, 2014 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 More sharing options...
justsomeguy Posted April 24, 2014 Share Posted April 24, 2014 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 More sharing options...
davej Posted April 24, 2014 Share Posted April 24, 2014 Elements have title properties (ie tooltips) but do events have them? Link to comment Share on other sites More sharing options...
justsomeguy Posted April 24, 2014 Share Posted April 24, 2014 That's not an event object, it's just a variable name. Link to comment Share on other sites More sharing options...
thescientist Posted April 25, 2014 Share Posted April 25, 2014 if(obj.property){ console.log('obj has property defined');} Link to comment Share on other sites More sharing options...
Ingolme Posted April 25, 2014 Share Posted April 25, 2014 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 More sharing options...
davej Posted April 25, 2014 Share Posted April 25, 2014 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 More sharing options...
thescientist Posted April 25, 2014 Share Posted April 25, 2014 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 More sharing options...
justsomeguy Posted April 25, 2014 Share Posted April 25, 2014 var t = events[1].1.title;Property names cannot be numeric. Link to comment Share on other sites More sharing options...
davej Posted April 25, 2014 Share Posted April 25, 2014 Yeah, non-numeric works. Link to comment Share on other sites More sharing options...
jimfog Posted April 28, 2014 Author Share Posted April 28, 2014 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 More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now