Ruby hash find base on key value and return an array
I have the following array of hashes:
{
"itens":
[
{"year": "2018", "right": true},
{"year": "2017", "right": true},
{"year": "2019", "right": false}
]
}
I need to find inside the hashes and return an array containing the year value. Only when the key "right" has value true.
[2018, 2017]
ruby
add a comment |
I have the following array of hashes:
{
"itens":
[
{"year": "2018", "right": true},
{"year": "2017", "right": true},
{"year": "2019", "right": false}
]
}
I need to find inside the hashes and return an array containing the year value. Only when the key "right" has value true.
[2018, 2017]
ruby
add a comment |
I have the following array of hashes:
{
"itens":
[
{"year": "2018", "right": true},
{"year": "2017", "right": true},
{"year": "2019", "right": false}
]
}
I need to find inside the hashes and return an array containing the year value. Only when the key "right" has value true.
[2018, 2017]
ruby
I have the following array of hashes:
{
"itens":
[
{"year": "2018", "right": true},
{"year": "2017", "right": true},
{"year": "2019", "right": false}
]
}
I need to find inside the hashes and return an array containing the year value. Only when the key "right" has value true.
[2018, 2017]
ruby
ruby
asked Nov 22 at 22:27
An j
1
1
add a comment |
add a comment |
4 Answers
4
active
oldest
votes
Given h your data structure
h[:itens].select { |item| item[:right] }.map { |item| item[:year].to_i }
You can add .uniq if you want each year just one time
add a comment |
Alternatively, looping only once:
h[:itens].each_with_object() { |i, arr| arr << i[:year].to_i if i[:right] }
# => [2018, 2017]
add a comment |
How about this:
items.select { |_| _['right'] }.map { |_| _['year'] }
_in block parameters is supposed to be used when a parameter is unused inside the block.
– Marcin Kołodziej
Nov 22 at 22:33
Depends on your coding style. Using_as a parameter when it’s the sole parameter, is considered valid and acceptable style.
– DjPadz
Nov 22 at 22:36
1
Yes,_is a valid local variable and therefore can be used within a block, but the main reason for using an underscore (or any string beginning with an underscore) as a block variable is to tell the reader that that that variable is not used in the block calculation, so no, it's not an acceptable style.
– Cary Swoveland
Nov 25 at 10:07
add a comment |
You can try this as well.
data[:itens].keep_if{|a| a[:right]}.map{|b| b[:year].to_i}
# => [2018, 2017]
Or, Alternatively
data[:itens].map{|a| a[:year].to_i if a[:right]}
# => [2018, 2017]
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53438644%2fruby-hash-find-base-on-key-value-and-return-an-array%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
Given h your data structure
h[:itens].select { |item| item[:right] }.map { |item| item[:year].to_i }
You can add .uniq if you want each year just one time
add a comment |
Given h your data structure
h[:itens].select { |item| item[:right] }.map { |item| item[:year].to_i }
You can add .uniq if you want each year just one time
add a comment |
Given h your data structure
h[:itens].select { |item| item[:right] }.map { |item| item[:year].to_i }
You can add .uniq if you want each year just one time
Given h your data structure
h[:itens].select { |item| item[:right] }.map { |item| item[:year].to_i }
You can add .uniq if you want each year just one time
edited Nov 22 at 22:48
answered Nov 22 at 22:29
Ursus
19.7k31329
19.7k31329
add a comment |
add a comment |
Alternatively, looping only once:
h[:itens].each_with_object() { |i, arr| arr << i[:year].to_i if i[:right] }
# => [2018, 2017]
add a comment |
Alternatively, looping only once:
h[:itens].each_with_object() { |i, arr| arr << i[:year].to_i if i[:right] }
# => [2018, 2017]
add a comment |
Alternatively, looping only once:
h[:itens].each_with_object() { |i, arr| arr << i[:year].to_i if i[:right] }
# => [2018, 2017]
Alternatively, looping only once:
h[:itens].each_with_object() { |i, arr| arr << i[:year].to_i if i[:right] }
# => [2018, 2017]
answered Nov 22 at 22:30
Marcin Kołodziej
4,191315
4,191315
add a comment |
add a comment |
How about this:
items.select { |_| _['right'] }.map { |_| _['year'] }
_in block parameters is supposed to be used when a parameter is unused inside the block.
– Marcin Kołodziej
Nov 22 at 22:33
Depends on your coding style. Using_as a parameter when it’s the sole parameter, is considered valid and acceptable style.
– DjPadz
Nov 22 at 22:36
1
Yes,_is a valid local variable and therefore can be used within a block, but the main reason for using an underscore (or any string beginning with an underscore) as a block variable is to tell the reader that that that variable is not used in the block calculation, so no, it's not an acceptable style.
– Cary Swoveland
Nov 25 at 10:07
add a comment |
How about this:
items.select { |_| _['right'] }.map { |_| _['year'] }
_in block parameters is supposed to be used when a parameter is unused inside the block.
– Marcin Kołodziej
Nov 22 at 22:33
Depends on your coding style. Using_as a parameter when it’s the sole parameter, is considered valid and acceptable style.
– DjPadz
Nov 22 at 22:36
1
Yes,_is a valid local variable and therefore can be used within a block, but the main reason for using an underscore (or any string beginning with an underscore) as a block variable is to tell the reader that that that variable is not used in the block calculation, so no, it's not an acceptable style.
– Cary Swoveland
Nov 25 at 10:07
add a comment |
How about this:
items.select { |_| _['right'] }.map { |_| _['year'] }
How about this:
items.select { |_| _['right'] }.map { |_| _['year'] }
answered Nov 22 at 22:31
DjPadz
3614
3614
_in block parameters is supposed to be used when a parameter is unused inside the block.
– Marcin Kołodziej
Nov 22 at 22:33
Depends on your coding style. Using_as a parameter when it’s the sole parameter, is considered valid and acceptable style.
– DjPadz
Nov 22 at 22:36
1
Yes,_is a valid local variable and therefore can be used within a block, but the main reason for using an underscore (or any string beginning with an underscore) as a block variable is to tell the reader that that that variable is not used in the block calculation, so no, it's not an acceptable style.
– Cary Swoveland
Nov 25 at 10:07
add a comment |
_in block parameters is supposed to be used when a parameter is unused inside the block.
– Marcin Kołodziej
Nov 22 at 22:33
Depends on your coding style. Using_as a parameter when it’s the sole parameter, is considered valid and acceptable style.
– DjPadz
Nov 22 at 22:36
1
Yes,_is a valid local variable and therefore can be used within a block, but the main reason for using an underscore (or any string beginning with an underscore) as a block variable is to tell the reader that that that variable is not used in the block calculation, so no, it's not an acceptable style.
– Cary Swoveland
Nov 25 at 10:07
_ in block parameters is supposed to be used when a parameter is unused inside the block.– Marcin Kołodziej
Nov 22 at 22:33
_ in block parameters is supposed to be used when a parameter is unused inside the block.– Marcin Kołodziej
Nov 22 at 22:33
Depends on your coding style. Using
_ as a parameter when it’s the sole parameter, is considered valid and acceptable style.– DjPadz
Nov 22 at 22:36
Depends on your coding style. Using
_ as a parameter when it’s the sole parameter, is considered valid and acceptable style.– DjPadz
Nov 22 at 22:36
1
1
Yes,
_ is a valid local variable and therefore can be used within a block, but the main reason for using an underscore (or any string beginning with an underscore) as a block variable is to tell the reader that that that variable is not used in the block calculation, so no, it's not an acceptable style.– Cary Swoveland
Nov 25 at 10:07
Yes,
_ is a valid local variable and therefore can be used within a block, but the main reason for using an underscore (or any string beginning with an underscore) as a block variable is to tell the reader that that that variable is not used in the block calculation, so no, it's not an acceptable style.– Cary Swoveland
Nov 25 at 10:07
add a comment |
You can try this as well.
data[:itens].keep_if{|a| a[:right]}.map{|b| b[:year].to_i}
# => [2018, 2017]
Or, Alternatively
data[:itens].map{|a| a[:year].to_i if a[:right]}
# => [2018, 2017]
add a comment |
You can try this as well.
data[:itens].keep_if{|a| a[:right]}.map{|b| b[:year].to_i}
# => [2018, 2017]
Or, Alternatively
data[:itens].map{|a| a[:year].to_i if a[:right]}
# => [2018, 2017]
add a comment |
You can try this as well.
data[:itens].keep_if{|a| a[:right]}.map{|b| b[:year].to_i}
# => [2018, 2017]
Or, Alternatively
data[:itens].map{|a| a[:year].to_i if a[:right]}
# => [2018, 2017]
You can try this as well.
data[:itens].keep_if{|a| a[:right]}.map{|b| b[:year].to_i}
# => [2018, 2017]
Or, Alternatively
data[:itens].map{|a| a[:year].to_i if a[:right]}
# => [2018, 2017]
answered Nov 23 at 9:49
Wysiati
1044
1044
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53438644%2fruby-hash-find-base-on-key-value-and-return-an-array%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown