How to get distinct values from an array of objects in JavaScript?

Assuming I have the following:

var array = 
    [
        {"name":"Joe", "age":17}, 
        {"name":"Bob", "age":17}, 
        {"name":"Carl", "age": 35}
    ]

What is the best way to be able to get an array of all of the distinct ages such that I get an result array of:

[17, 35]

Is there some way I could alternatively structure the data or better method such that I would not have to iterate through each array checking the value of “age” and check against another array for its existence, and add it if not?

If there was some way I could just pull out the distinct ages without iterating…

Current inefficent way I would like to improve… If it means that instead of “array” being an array of objects, but a “map” of objects with some unique key (i.e. “1,2,3”) that would be okay too. Im just looking for the most performance efficient way.

The following is how I currently do it, but for me, iteration appears to just be crummy for efficiency even though it does work…

var distinct = []
for (var i = 0; i < array.length; i++)
   if (array[i].age not in distinct)
      distinct.push(array[i].age)

145 thoughts on “How to get distinct values from an array of objects in JavaScript?”

  1. My brother recommended I would possibly like this website.
    He was once totally right. This publish truly made my day.
    You cann’t consider simply how a lot time I had spent for this
    information! Thanks!

    Reply
  2. Hey there! I’ve been following your blog for some time now and finally got the courage to go ahead and give you a shout out from New Caney
    Tx! Just wanted to say keep up the good work!

    Reply
  3. Pingback: viagra effects
  4. Hi! I’m at work browsing your blog from my new iphone 3gs! Just wanted to say I love reading your blog and look forward to all your posts!
    Keep up the excellent work!

    Reply
  5. I’m now not certain the place you’re getting your information, but great topic.

    I needs to spend some time learning much more or understanding more.
    Thanks for magnificent information I was searching for this info for my mission.

    Reply
  6. Its such as you read my thoughts! You appear to know so much approximately this, like you wrote
    the guide in it or something. I feel that you just could do with some p.c.
    to power the message home a bit, but other than that, this is magnificent blog.
    An excellent read. I’ll certainly be back.

    Reply
  7. Hello! I’ve been reading your weblog for some time now and finally got the
    bravery to go ahead and give you a shout out from Austin Texas!
    Just wanted to mention keep up the fantastic job!

    Reply
  8. Pingback: viagra by mail
  9. Pingback: sildenafil 50mg uk
  10. Pingback: mexican viagra
  11. great put up, very informative. I’m wondering why the opposite specialists of this sector don’t notice this.
    You must continue your writing. I’m confident, you’ve a
    huge readers’ base already!

    Reply
  12. Hello there, I do think your site could possibly be having
    internet browser compatibility issues. Whenever I
    look at your website in Safari, it looks fine however,
    when opening in I.E., it’s got some overlapping issues. I
    simply wanted to provide you with a quick heads up! Besides that,
    wonderful blog!

    Reply
  13. Woah! I’m really loving the template/theme of this
    website. It’s simple, yet effective. A lot of times it’s difficult to get that “perfect balance” between user friendliness and appearance.

    I must say you have done a superb job with this.
    In addition, the blog loads very fast for me on Safari.
    Exceptional Blog!

    Reply
  14. Pingback: amlodipine use
  15. Pingback: cialis commercial
  16. Pingback: metformin recall
  17. Link exchange is nothing else but it is only placing the other person’s blog link on your page at
    appropriate place and other person will also do similar in support of you.

    Reply
  18. I do believe all of the ideas you’ve offered
    for your post. They’re very convincing and will certainly work.

    Still, the posts are very brief for newbies. May you please extend them a little from next time?
    Thanks for the post.

    Reply
  19. I’m not sure exactly why but this weblog is loading very slow for me.
    Is anyone else having this problem or is it a problem on my end?
    I’ll check back later on and see if the problem still
    exists.

    Reply
  20. Though it’s not hollow. http://fitnessdom.ru/ Erectile dysfunction, psychological factors ran ing from time. This is a sign of emotional symptoms of ED. It also be a sign of nerve signals reach the symptoms of a sign of stress. Frequent ED, blood flow into your penis. Blood flow is define Erectile dysfunction about the causes of ED. You may be reluctant to get or direct contact with erections from time isn’t necessarily a sign of emotional states that firm enough for concern. If erectile dysfunction (ED) is the result o increased blood flow into two chambers inside the erection can flow out through the penis.

    Reply
  21. Pingback: lasix 20 mg price
  22. Pingback: orlistat
  23. I was very happy to find this site. I wanted to thank you
    for your time for this fantastic read!! I definitely appreciated every little bit of it and I have you
    bookmarked to see new stuff on your blog.

    Reply
  24. First off I want to say superb blog! I had a
    quick question in which I’d like to ask if you do not mind.
    I was interested to find out how you center yourself and clear your mind prior to writing.
    I’ve had a hard time clearing my mind in getting my thoughts out there.
    I truly do enjoy writing but it just seems like the first 10
    to 15 minutes tend to be wasted simply just trying to figure
    out how to begin. Any suggestions or hints?
    Cheers!

    Reply
  25. What’s Taking place i am new to this, I stumbled upon this I have found It positively useful and it
    has aided me out loads. I’m hoping to give a contribution & assist other customers like its helped me.
    Great job.

    Reply
  26. I believe what you said was actually very logical. However, what about this?

    suppose you were to create a killer title? I mean,
    I don’t want to tell you how to run your website, but suppose you added
    a post title that grabbed people’s attention? I mean How to get distinct
    values from an array of objects in JavaScript? – JavaScript Help is a little plain. You might look at Yahoo’s home page and watch how they create news headlines to get people to click.
    You might add a video or a related pic or two to grab readers
    excited about what you’ve got to say. In my opinion, it would bring your posts a little bit
    more interesting.

    Reply
  27. Hi my loved one! I wish to say that this post is amazing,
    nice written and come with almost all important infos.
    I’d like to look extra posts like this .

    Reply
  28. hey there and thank you for your info – I’ve certainly picked up anything new from right here.
    I did however expertise some technical points using this site, since
    I experienced to reload the website lots of times previous to I could get it to load
    properly. I had been wondering if your web host is OK?
    Not that I am complaining, but sluggish loading instances
    times will sometimes affect your placement in google and can damage your high-quality score if ads and
    marketing with Adwords. Anyway I am adding this RSS to my e-mail and can look out for
    a lot more of your respective interesting content. Make sure you update
    this again very soon.

    Reply
  29. Please let me know if you’re looking for a article author for your blog.
    You have some really good articles and I think I would be
    a good asset. If you ever want to take some of the load off,
    I’d love to write some content for your blog in exchange for a link back to mine.

    Please shoot me an e-mail if interested. Kudos!

    Reply
  30. Does your site have a contact page? I’m having trouble locating it but, I’d like to shoot
    you an e-mail. I’ve got some suggestions for your blog you might be interested in hearing.
    Either way, great website and I look forward to seeing it improve over time.

    Reply
  31. Heya just wanted to give you a brief heads up and let you know a few of the pictures
    aren’t loading correctly. I’m not sure why but I think its a linking issue.

    I’ve tried it in two different browsers and both show the
    same results.

    Reply
  32. I’ve been exploring for a little for any high quality articles
    or blog posts on this sort of space . Exploring in Yahoo I finally stumbled upon this website.
    Studying this info So i’m happy to convey that I’ve an incredibly good uncanny
    feeling I discovered just what I needed. I such a
    lot without a doubt will make sure to do not overlook this website and provides it a look on a
    continuing basis.

    Reply
  33. With havin so much written content do you ever run into any problems of plagorism
    or copyright violation? My site has a lot of unique content I’ve either authored myself or outsourced but it looks like a lot of it is popping it up
    all over the web without my permission. Do you know any
    techniques to help reduce content from being ripped off?

    I’d truly appreciate it.

    Reply
  34. Aw, this was an extremely nice post. Finding the time and actual effort to generate a really good
    article… but what can I say… I procrastinate a lot and never seem to get
    anything done.

    Reply
  35. Write more, thats all I have to say. Literally, it seems as
    though you relied on the video to make your point.
    You obviously know what youre talking about, why waste your intelligence on just
    posting videos to your weblog when you could be giving us something informative to read?

    Reply
  36. I don’t even know the way I finished up here,
    however I believed this publish used to be great. I do not recognise who you might be however
    definitely you are going to a famous blogger if you happen to are not already.
    Cheers!

    Reply
  37. I’m amazed, I have to admit. Rarely do I encounter a blog that’s both educative and interesting,
    and without a doubt, you’ve hit the nail on the head.
    The problem is something which not enough folks are speaking
    intelligently about. I’m very happy that I came across this during my
    search for something regarding this.

    Reply
  38. I absolutely love your site.. Pleasant colors & theme.

    Did you develop this site yourself? Please reply back as I’m wanting to create my own website and would
    like to learn where you got this from or what the theme
    is named. Cheers!

    Reply
  39. Today, I went to the beach with my kids. I found a sea shell and gave it to my 4 year
    old daughter and said “You can hear the ocean if you put this to your ear.”
    She placed the shell to her ear and screamed.

    There was a hermit crab inside and it pinched her ear. She never wants to
    go back! LoL I know this is totally off topic but I had to tell
    someone!

    Reply
  40. I am curious to find out what blog platform you have been working with?
    I’m having some small security issues with my latest blog and I’d
    like to find something more secure. Do you have any suggestions?

    Reply
  41. With havin so much content and articles do you ever run into any problems of plagorism or copyright violation? My website has
    a lot of unique content I’ve either written myself or outsourced but it looks
    like a lot of it is popping it up all over the web without my agreement.
    Do you know any ways to help protect against content from being stolen? I’d truly appreciate it.

    Reply
  42. Thank you for some other informative site.
    The place else could I get that type of info written in such an ideal method?

    I have a mission that I’m just now operating on, and I
    have been on the glance out for such information.

    Reply
  43. Hi! I know this is somewhat off topic but I was
    wondering if you knew where I could locate a captcha plugin for my
    comment form? I’m using the same blog platform as yours and I’m having problems
    finding one? Thanks a lot!

    Reply
  44. Nice post. I learn something totally new and challenging
    on sites I stumbleupon every day. It’s always
    exciting to read through articles from other writers and practice something from other sites.

    Reply
  45. Do you mind if I quote a few of your articles as long as I provide credit
    and sources back to your webpage? My blog site is in the very same area of interest as yours and my visitors would genuinely benefit from some of the information you provide here.
    Please let me know if this alright with you. Thank you!

    Reply
  46. I was curious if you ever thought of changing the layout of your site?
    Its very well written; I love what youve got to say. But maybe you could a little more in the way of content so people could connect with
    it better. Youve got an awful lot of text for only having 1 or 2 pictures.
    Maybe you could space it out better?

    Reply
  47. Magnificent beat ! I wish to apprentice at the same time as you amend your website,
    how could i subscribe for a blog site? The account helped me a acceptable deal.
    I have been tiny bit familiar of this your broadcast provided bright clear concept

    Reply
  48. Hey are using WordPress for your site platform? I’m new to the blog world but I’m trying to get started and create my own. Do you need any
    coding expertise to make your own blog? Any help would be really appreciated!

    Reply
  49. I seriously love your site.. Pleasant colors & theme.
    Did you develop this amazing site yourself? Please reply back as I’m attempting
    to create my very own website and want to find out where you got
    this from or what the theme is called. Appreciate it!

    Reply
  50. you are in reality a just right webmaster. The
    site loading pace is incredible. It sort of feels that you’re doing any
    distinctive trick. In addition, The contents are
    masterwork. you have performed a great process on this matter!

    Reply
  51. hey there and thank you for your info – I’ve definitely
    picked up anything new from right here. I did however expertise
    a few technical issues using this website, since I experienced to
    reload the site a lot of times previous to I could get it to load
    correctly. I had been wondering if your web hosting is OK?
    Not that I am complaining, but slow loading instances times will often affect your placement in google and could damage
    your high-quality score if ads and marketing with Adwords.
    Well I am adding this RSS to my e-mail and can look out for much more of your respective fascinating content.

    Ensure that you update this again very soon.

    Reply
  52. Generally I don’t learn article on blogs, but I wish to say that this
    write-up very pressured me to take a look at and do so!

    Your writing style has been amazed me. Thank you, quite great post.

    Reply
  53. Hello There. I found your blog using msn. This is an extremely well written article.

    I will be sure to bookmark it and return to read more of your useful information. Thanks for the
    post. I’ll definitely comeback.

    Reply
  54. I just could not leave your web site prior to suggesting that I really
    enjoyed the usual info an individual provide to your visitors?
    Is gonna be back often in order to inspect new posts

    Reply
  55. Pingback: cialis erection
  56. Pingback: viagra warnings

Leave a Comment