Removing square brackets from hash array for value
up vote
2
down vote
favorite
I am having a following hash array
A = [{"name" => ["xx"], "status" => ["true"]}, {"name" => ["yy"], "status" => ["true"]}
I tried following code to remove the square brackets
A.to_s.gsub("\[|\]", "")
also tried with code
p A.map { |hash| hash.each_with_object({}) { |(k, v), hash| hash[k] = v.first } }
but its not working.
How I remove the square brackets to get following output
A = [{"name" => "xx", "status" => "true"}, {"name" => "yy", "status" => "true"}
Kindly assist
ruby hash
add a comment |
up vote
2
down vote
favorite
I am having a following hash array
A = [{"name" => ["xx"], "status" => ["true"]}, {"name" => ["yy"], "status" => ["true"]}
I tried following code to remove the square brackets
A.to_s.gsub("\[|\]", "")
also tried with code
p A.map { |hash| hash.each_with_object({}) { |(k, v), hash| hash[k] = v.first } }
but its not working.
How I remove the square brackets to get following output
A = [{"name" => "xx", "status" => "true"}, {"name" => "yy", "status" => "true"}
Kindly assist
ruby hash
add a comment |
up vote
2
down vote
favorite
up vote
2
down vote
favorite
I am having a following hash array
A = [{"name" => ["xx"], "status" => ["true"]}, {"name" => ["yy"], "status" => ["true"]}
I tried following code to remove the square brackets
A.to_s.gsub("\[|\]", "")
also tried with code
p A.map { |hash| hash.each_with_object({}) { |(k, v), hash| hash[k] = v.first } }
but its not working.
How I remove the square brackets to get following output
A = [{"name" => "xx", "status" => "true"}, {"name" => "yy", "status" => "true"}
Kindly assist
ruby hash
I am having a following hash array
A = [{"name" => ["xx"], "status" => ["true"]}, {"name" => ["yy"], "status" => ["true"]}
I tried following code to remove the square brackets
A.to_s.gsub("\[|\]", "")
also tried with code
p A.map { |hash| hash.each_with_object({}) { |(k, v), hash| hash[k] = v.first } }
but its not working.
How I remove the square brackets to get following output
A = [{"name" => "xx", "status" => "true"}, {"name" => "yy", "status" => "true"}
Kindly assist
ruby hash
ruby hash
edited Nov 8 at 19:32
asked Nov 8 at 18:48
Jinx
236
236
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
3
down vote
accepted
Since they're strings inside arrays, the is the representation Ruby does of it. Try accessing the first element for each key's value in those hashes:
a = [{"name" => ["xx"], "status" => ["true"]}, {"name" => ["yy"], "status" => ["true"]}]
p a.map { |hash| hash.transform_values(&:first) }
# [{"name"=>"xx", "status"=>"true"}, {"name"=>"yy", "status"=>"true"}]
Depending on your Ruby version, you might not have transform_values available. A simple each_with_object
would work similarly in that case:
p a.map { |hash| hash.each_with_object({}) { |(k, v), hash| hash[k] = v.first } }
# [{"name"=>"xx", "status"=>"true"}, {"name"=>"yy", "status"=>"true"}]
Its throwing me undefined method `transform_values' for {"name"=>["xx"], "status"=>["true"]}:Hash (NoMethodError)
– Jinx
Nov 8 at 18:59
Sorry, it depends in your Ruby version. See the updated answer.
– Sebastian Palma
Nov 8 at 19:03
I am not sure what I did wrong but its still giving me response as A = [{"name" => ["xx"], "status" => ["true"]}, {"name" => ["yy"], "status" => ["true"]}
– Jinx
Nov 8 at 19:29
Can you add your attempt to the question?
– Sebastian Palma
Nov 8 at 19:30
updated the question.
– Jinx
Nov 8 at 19:33
|
show 6 more comments
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
3
down vote
accepted
Since they're strings inside arrays, the is the representation Ruby does of it. Try accessing the first element for each key's value in those hashes:
a = [{"name" => ["xx"], "status" => ["true"]}, {"name" => ["yy"], "status" => ["true"]}]
p a.map { |hash| hash.transform_values(&:first) }
# [{"name"=>"xx", "status"=>"true"}, {"name"=>"yy", "status"=>"true"}]
Depending on your Ruby version, you might not have transform_values available. A simple each_with_object
would work similarly in that case:
p a.map { |hash| hash.each_with_object({}) { |(k, v), hash| hash[k] = v.first } }
# [{"name"=>"xx", "status"=>"true"}, {"name"=>"yy", "status"=>"true"}]
Its throwing me undefined method `transform_values' for {"name"=>["xx"], "status"=>["true"]}:Hash (NoMethodError)
– Jinx
Nov 8 at 18:59
Sorry, it depends in your Ruby version. See the updated answer.
– Sebastian Palma
Nov 8 at 19:03
I am not sure what I did wrong but its still giving me response as A = [{"name" => ["xx"], "status" => ["true"]}, {"name" => ["yy"], "status" => ["true"]}
– Jinx
Nov 8 at 19:29
Can you add your attempt to the question?
– Sebastian Palma
Nov 8 at 19:30
updated the question.
– Jinx
Nov 8 at 19:33
|
show 6 more comments
up vote
3
down vote
accepted
Since they're strings inside arrays, the is the representation Ruby does of it. Try accessing the first element for each key's value in those hashes:
a = [{"name" => ["xx"], "status" => ["true"]}, {"name" => ["yy"], "status" => ["true"]}]
p a.map { |hash| hash.transform_values(&:first) }
# [{"name"=>"xx", "status"=>"true"}, {"name"=>"yy", "status"=>"true"}]
Depending on your Ruby version, you might not have transform_values available. A simple each_with_object
would work similarly in that case:
p a.map { |hash| hash.each_with_object({}) { |(k, v), hash| hash[k] = v.first } }
# [{"name"=>"xx", "status"=>"true"}, {"name"=>"yy", "status"=>"true"}]
Its throwing me undefined method `transform_values' for {"name"=>["xx"], "status"=>["true"]}:Hash (NoMethodError)
– Jinx
Nov 8 at 18:59
Sorry, it depends in your Ruby version. See the updated answer.
– Sebastian Palma
Nov 8 at 19:03
I am not sure what I did wrong but its still giving me response as A = [{"name" => ["xx"], "status" => ["true"]}, {"name" => ["yy"], "status" => ["true"]}
– Jinx
Nov 8 at 19:29
Can you add your attempt to the question?
– Sebastian Palma
Nov 8 at 19:30
updated the question.
– Jinx
Nov 8 at 19:33
|
show 6 more comments
up vote
3
down vote
accepted
up vote
3
down vote
accepted
Since they're strings inside arrays, the is the representation Ruby does of it. Try accessing the first element for each key's value in those hashes:
a = [{"name" => ["xx"], "status" => ["true"]}, {"name" => ["yy"], "status" => ["true"]}]
p a.map { |hash| hash.transform_values(&:first) }
# [{"name"=>"xx", "status"=>"true"}, {"name"=>"yy", "status"=>"true"}]
Depending on your Ruby version, you might not have transform_values available. A simple each_with_object
would work similarly in that case:
p a.map { |hash| hash.each_with_object({}) { |(k, v), hash| hash[k] = v.first } }
# [{"name"=>"xx", "status"=>"true"}, {"name"=>"yy", "status"=>"true"}]
Since they're strings inside arrays, the is the representation Ruby does of it. Try accessing the first element for each key's value in those hashes:
a = [{"name" => ["xx"], "status" => ["true"]}, {"name" => ["yy"], "status" => ["true"]}]
p a.map { |hash| hash.transform_values(&:first) }
# [{"name"=>"xx", "status"=>"true"}, {"name"=>"yy", "status"=>"true"}]
Depending on your Ruby version, you might not have transform_values available. A simple each_with_object
would work similarly in that case:
p a.map { |hash| hash.each_with_object({}) { |(k, v), hash| hash[k] = v.first } }
# [{"name"=>"xx", "status"=>"true"}, {"name"=>"yy", "status"=>"true"}]
edited Nov 8 at 19:05
answered Nov 8 at 18:53
Sebastian Palma
15.2k41933
15.2k41933
Its throwing me undefined method `transform_values' for {"name"=>["xx"], "status"=>["true"]}:Hash (NoMethodError)
– Jinx
Nov 8 at 18:59
Sorry, it depends in your Ruby version. See the updated answer.
– Sebastian Palma
Nov 8 at 19:03
I am not sure what I did wrong but its still giving me response as A = [{"name" => ["xx"], "status" => ["true"]}, {"name" => ["yy"], "status" => ["true"]}
– Jinx
Nov 8 at 19:29
Can you add your attempt to the question?
– Sebastian Palma
Nov 8 at 19:30
updated the question.
– Jinx
Nov 8 at 19:33
|
show 6 more comments
Its throwing me undefined method `transform_values' for {"name"=>["xx"], "status"=>["true"]}:Hash (NoMethodError)
– Jinx
Nov 8 at 18:59
Sorry, it depends in your Ruby version. See the updated answer.
– Sebastian Palma
Nov 8 at 19:03
I am not sure what I did wrong but its still giving me response as A = [{"name" => ["xx"], "status" => ["true"]}, {"name" => ["yy"], "status" => ["true"]}
– Jinx
Nov 8 at 19:29
Can you add your attempt to the question?
– Sebastian Palma
Nov 8 at 19:30
updated the question.
– Jinx
Nov 8 at 19:33
Its throwing me undefined method `transform_values' for {"name"=>["xx"], "status"=>["true"]}:Hash (NoMethodError)
– Jinx
Nov 8 at 18:59
Its throwing me undefined method `transform_values' for {"name"=>["xx"], "status"=>["true"]}:Hash (NoMethodError)
– Jinx
Nov 8 at 18:59
Sorry, it depends in your Ruby version. See the updated answer.
– Sebastian Palma
Nov 8 at 19:03
Sorry, it depends in your Ruby version. See the updated answer.
– Sebastian Palma
Nov 8 at 19:03
I am not sure what I did wrong but its still giving me response as A = [{"name" => ["xx"], "status" => ["true"]}, {"name" => ["yy"], "status" => ["true"]}
– Jinx
Nov 8 at 19:29
I am not sure what I did wrong but its still giving me response as A = [{"name" => ["xx"], "status" => ["true"]}, {"name" => ["yy"], "status" => ["true"]}
– Jinx
Nov 8 at 19:29
Can you add your attempt to the question?
– Sebastian Palma
Nov 8 at 19:30
Can you add your attempt to the question?
– Sebastian Palma
Nov 8 at 19:30
updated the question.
– Jinx
Nov 8 at 19:33
updated the question.
– Jinx
Nov 8 at 19:33
|
show 6 more comments
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%2f53214287%2fremoving-square-brackets-from-hash-array-for-value%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