{"id":2343,"date":"2015-11-27T13:45:04","date_gmt":"2015-11-27T13:45:04","guid":{"rendered":"http:\/\/www.nikola-breznjak.com\/blog\/?p=2343"},"modified":"2015-11-27T13:52:41","modified_gmt":"2015-11-27T13:52:41","slug":"lodash","status":"publish","type":"post","link":"https:\/\/nikola-breznjak.com\/blog\/stack-overflow\/lodash\/","title":{"rendered":"How to get nested object property with pluck in Lodash"},"content":{"rendered":"<p><a href=\"http:\/\/stackoverflow.com\/users\/534755\/nikola\"><img loading=\"lazy\" decoding=\"async\" title=\"profile for Nikola at Stack Overflow, Q&amp;A for professional and enthusiast programmers\" src=\"http:\/\/stackoverflow.com\/users\/flair\/534755.png\" rel=\"lightbox[2343]\" alt=\"profile for Nikola at Stack Overflow, Q&amp;A for professional and enthusiast programmers\" width=\"208\" height=\"58\" \/><\/a><br \/>\nI&#8217;m a big fan of <a href=\"http:\/\/stackoverflow.com\/\">Stack Overflow<\/a> and I tend to contribute regularly (am currently in the <a href=\"http:\/\/stackexchange.com\/leagues\/1\/alltime\/stackoverflow\/2008-07-31\/534755?sort=reputationchange#534755\">top 0.X%<\/a>).\u00a0In this category (<a href=\"http:\/\/www.nikola-breznjak.com\/blog\/category\/stack-overflow\/\">stackoverflow<\/a>)\u00a0of posts, I will be posting my top rated questions and answers. This, btw, is allowed as explained in the meta thread <a href=\"http:\/\/meta.stackoverflow.com\/questions\/266971\/can-i-post-so-questions-and-answers-in-a-personal-blog\/266973\">here<\/a>.<\/p>\n<p>This Lodash <a href=\"http:\/\/stackoverflow.com\/questions\/30709041\/how-to-design-a-two-column-button-in-ionic-framework\">question<\/a> was actually asked by myself:<\/p>\n<p>I have an array of objects like this:<\/p>\n<pre class=\"lang:default decode:true\">var characters = [\r\n    { 'name': 'barney',  'age': 36, 'salary':{'amount': 10} },\r\n    { 'name': 'fred',    'age': 40, 'salary':{'amount': 20} },\r\n    { 'name': 'pebbles', 'age': 1,  'salary':{'amount': 30} }\r\n];<\/pre>\n<p>I want to get the salary amounts into an array. I managed to do it by chaining two <code>pluck<\/code> functions, like this:<\/p>\n<pre class=\"lang:default decode:true\">var salaries = _(characters)\r\n  .pluck('salary')\r\n  .pluck('amount')\r\n  .value();\r\n\r\nconsole.log(salaries); \/\/[10, 20, 30]<\/pre>\n<p>Is there a way to do this by using only one <code>pluck<\/code>? Is there a better way with some other function in lodash?<\/p>\n<p>The answer, from user <a href=\"http:\/\/stackoverflow.com\/users\/1903116\/thefourtheye\">thefourtheye<\/a>, was:<\/p>\n<blockquote><p>You can just give the path to be used as a string, like this<\/p>\n<pre class=\"lang:default decode:true \">console.log(_(characters).pluck('salary.amount').value())\r\n\/\/ [ 10, 20, 30 ]<\/pre>\n<p>Or use it directly<\/p>\n<pre class=\"lang:default decode:true\">console.log(_.pluck(characters, 'salary.amount'));\r\n\/\/ [ 10, 20, 30 ]<\/pre>\n<p>https:\/\/twitter.com\/HitmanHR\/status\/670237244695977984<\/p><\/blockquote>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;m a big fan of Stack Overflow and I tend to contribute regularly (am currently in the top 0.X%).\u00a0In this category (stackoverflow)\u00a0of posts, I will be posting my&hellip;<\/p>\n","protected":false},"author":1,"featured_media":609,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[35],"tags":[],"class_list":["post-2343","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-stack-overflow"],"_links":{"self":[{"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/posts\/2343","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/comments?post=2343"}],"version-history":[{"count":4,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/posts\/2343\/revisions"}],"predecessor-version":[{"id":2347,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/posts\/2343\/revisions\/2347"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/media\/609"}],"wp:attachment":[{"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/media?parent=2343"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/categories?post=2343"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/tags?post=2343"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}