Ruby - Best way to map objects into one array with fixed keySerializing to JSON in jQueryCan you use a trailing comma in a JSON object?How do I format a Microsoft JSON date?Convert form data to JavaScript object with jQueryHow to sum array of numbers in Ruby?Check if a value exists in an array in RubySort hash by key, return hash in RubyRuby objects and JSON serialization (without Rails)Map and Remove nil values in RubyDetermining type of an object in ruby
On what legal basis did the UK remove the 'European Union' from its passport?
Why do Thanos's punches not kill Captain America or at least cause some mortal injuries?
Anatomically Correct Carnivorous Tree
Reaction of borax with NaOH
c++ what does , means after if
Can the sorting of a list be verified without comparing neighbors?
What to do if SUS scores contradict qualitative feedback?
What is Plautus’s pun about frustum and frustrum?
Why in a Ethernet LAN, a packet sniffer can obtain all packets sent over the LAN?
List software from restricted, multiverse separately
How to compact two the parabol commands in the following example?
How can this triangle figure be modeled/drawn with TikZ?
Who was this character from the Tomb of Annihilation adventure before they became a monster?
use the oversamplling followed by '' decimation method ''to increasee the ADC resolution and not normal averaging
What does i386 mean on macOS Mojave?
Was this a power play by Daenerys?
How does Howard Stark know this?
Early arrival in Australia, early hotel check in not available
Unable to execute two commands in Startup Applications
Why was Thor doubtful about his worthiness to Mjolnir?
Size of a folder with du
How do I compare the result of "1d20+x, with advantage" to "1d20+y, without advantage", assuming x < y?
How to Access data returned from Apex class in JS controller using Lightning web component
Extrude the faces of a cube symmetrically along XYZ
Ruby - Best way to map objects into one array with fixed key
Serializing to JSON in jQueryCan you use a trailing comma in a JSON object?How do I format a Microsoft JSON date?Convert form data to JavaScript object with jQueryHow to sum array of numbers in Ruby?Check if a value exists in an array in RubySort hash by key, return hash in RubyRuby objects and JSON serialization (without Rails)Map and Remove nil values in RubyDetermining type of an object in ruby
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
I try to merge some object into one array
I did output by
q = [["99","99","99"],["9"]]
o = [["b","1"],["c","3"],["d","1"],["c","30"]]
puts q.zip(o).map .to_json
=> [["99",["b","1"]],["99",["c","3"]],["99",["d","1"]],["9",["c","30"]]]
i'm looking for best way to
["99"=>"b"=>"1", "c"=>"3", "d"=>"1",{"9"=>"c"=>"30"]
json ruby
|
show 5 more comments
I try to merge some object into one array
I did output by
q = [["99","99","99"],["9"]]
o = [["b","1"],["c","3"],["d","1"],["c","30"]]
puts q.zip(o).map .to_json
=> [["99",["b","1"]],["99",["c","3"]],["99",["d","1"]],["9",["c","30"]]]
i'm looking for best way to
["99"=>"b"=>"1", "c"=>"3", "d"=>"1",{"9"=>"c"=>"30"]
json ruby
1
Your "some array" is invalid.
– sawa
Mar 22 at 9:25
yes, I'm still fresher, so I don't know to call this, but I print with this output and I try to beautify this output :(
– Tho Nguyen
Mar 22 at 9:29
No, it is not reproducible. Your initial array is invalid.
– sawa
Mar 22 at 9:34
I have updated my question, for more my flow to do that output. Can you teach me how to make it smarter :(
– Tho Nguyen
Mar 22 at 9:39
3
Where does that"10"come from in the expected output?
– Stefan
Mar 22 at 9:42
|
show 5 more comments
I try to merge some object into one array
I did output by
q = [["99","99","99"],["9"]]
o = [["b","1"],["c","3"],["d","1"],["c","30"]]
puts q.zip(o).map .to_json
=> [["99",["b","1"]],["99",["c","3"]],["99",["d","1"]],["9",["c","30"]]]
i'm looking for best way to
["99"=>"b"=>"1", "c"=>"3", "d"=>"1",{"9"=>"c"=>"30"]
json ruby
I try to merge some object into one array
I did output by
q = [["99","99","99"],["9"]]
o = [["b","1"],["c","3"],["d","1"],["c","30"]]
puts q.zip(o).map .to_json
=> [["99",["b","1"]],["99",["c","3"]],["99",["d","1"]],["9",["c","30"]]]
i'm looking for best way to
["99"=>"b"=>"1", "c"=>"3", "d"=>"1",{"9"=>"c"=>"30"]
json ruby
json ruby
edited Mar 23 at 11:19
Tho Nguyen
asked Mar 22 at 9:21
Tho NguyenTho Nguyen
116
116
1
Your "some array" is invalid.
– sawa
Mar 22 at 9:25
yes, I'm still fresher, so I don't know to call this, but I print with this output and I try to beautify this output :(
– Tho Nguyen
Mar 22 at 9:29
No, it is not reproducible. Your initial array is invalid.
– sawa
Mar 22 at 9:34
I have updated my question, for more my flow to do that output. Can you teach me how to make it smarter :(
– Tho Nguyen
Mar 22 at 9:39
3
Where does that"10"come from in the expected output?
– Stefan
Mar 22 at 9:42
|
show 5 more comments
1
Your "some array" is invalid.
– sawa
Mar 22 at 9:25
yes, I'm still fresher, so I don't know to call this, but I print with this output and I try to beautify this output :(
– Tho Nguyen
Mar 22 at 9:29
No, it is not reproducible. Your initial array is invalid.
– sawa
Mar 22 at 9:34
I have updated my question, for more my flow to do that output. Can you teach me how to make it smarter :(
– Tho Nguyen
Mar 22 at 9:39
3
Where does that"10"come from in the expected output?
– Stefan
Mar 22 at 9:42
1
1
Your "some array" is invalid.
– sawa
Mar 22 at 9:25
Your "some array" is invalid.
– sawa
Mar 22 at 9:25
yes, I'm still fresher, so I don't know to call this, but I print with this output and I try to beautify this output :(
– Tho Nguyen
Mar 22 at 9:29
yes, I'm still fresher, so I don't know to call this, but I print with this output and I try to beautify this output :(
– Tho Nguyen
Mar 22 at 9:29
No, it is not reproducible. Your initial array is invalid.
– sawa
Mar 22 at 9:34
No, it is not reproducible. Your initial array is invalid.
– sawa
Mar 22 at 9:34
I have updated my question, for more my flow to do that output. Can you teach me how to make it smarter :(
– Tho Nguyen
Mar 22 at 9:39
I have updated my question, for more my flow to do that output. Can you teach me how to make it smarter :(
– Tho Nguyen
Mar 22 at 9:39
3
3
Where does that
"10" come from in the expected output?– Stefan
Mar 22 at 9:42
Where does that
"10" come from in the expected output?– Stefan
Mar 22 at 9:42
|
show 5 more comments
3 Answers
3
active
oldest
votes
a = [["9",["b","8"]],["9",["c","2"]],["9",["d","6"]]]
a.group_by(&:first).transform_values
# => "9"=>"b"=>"8", "c"=>"2", "d"=>"6"
a.group_by(&:first).transform_values.map k => v
# => ["9"=>"b"=>"8", "c"=>"2", "d"=>"6"]
add a comment |
Looking for something like this?:
some_array = [["9",["b","8"]], ["9",["c","2"]], ["9",["d","6"]]]
some_hash = some_array.each_with_object(Hash.new ) do |(k, (sub_key, sub_val)), hash|
hash[k][sub_key] = sub_val
end
p some_hash
#=> "9"=>"b"=>"8", "c"=>"2", "d"=>"6"
add a comment |
After the question was updated:
q = [["99","99","99"],["9"]]
o = [["b","1"],["c","3"],["d","1"],["c","30"]]
q.flatten.map.with_index.with_object(Hash.new ) .transform_values(&:to_h)
#=> "99"=>"b"=>"1", "c"=>"3", "d"=>"1", "9"=>"c"=>"30"
I have update my question, for full output. Thank @iGian :)
– Tho Nguyen
Mar 23 at 11:13
[["100",["b","5"]],["101",["d","5"]],["102",["b","5"]],["103",["b","5"]],["104",["b","5"]],["105",["c","5"]],["106",["a","4"]],["106",["b","1"]],["107",["a","5"]],["108",["a","1"]],["108",["b","3"]],["108",["c","1"]],["109",["a","2"]],["109",["b","2"]],["109",["d","1"]]] I have cut output for you, I think we need edit q to another difference with first
– Tho Nguyen
Mar 23 at 11:28
@ThoNguyen, please, check my edit. I started from the original arrays.
– iGian
Mar 23 at 11:35
Thank you, I think we can make it to an object is good for reusable :)
– Tho Nguyen
Mar 23 at 11:35
Sure, your solution can solved my problem :)
– Tho Nguyen
Mar 23 at 11:37
|
show 2 more comments
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%2f55296417%2fruby-best-way-to-map-objects-into-one-array-with-fixed-key%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
a = [["9",["b","8"]],["9",["c","2"]],["9",["d","6"]]]
a.group_by(&:first).transform_values
# => "9"=>"b"=>"8", "c"=>"2", "d"=>"6"
a.group_by(&:first).transform_values.map k => v
# => ["9"=>"b"=>"8", "c"=>"2", "d"=>"6"]
add a comment |
a = [["9",["b","8"]],["9",["c","2"]],["9",["d","6"]]]
a.group_by(&:first).transform_values
# => "9"=>"b"=>"8", "c"=>"2", "d"=>"6"
a.group_by(&:first).transform_values.map k => v
# => ["9"=>"b"=>"8", "c"=>"2", "d"=>"6"]
add a comment |
a = [["9",["b","8"]],["9",["c","2"]],["9",["d","6"]]]
a.group_by(&:first).transform_values
# => "9"=>"b"=>"8", "c"=>"2", "d"=>"6"
a.group_by(&:first).transform_values.map k => v
# => ["9"=>"b"=>"8", "c"=>"2", "d"=>"6"]
a = [["9",["b","8"]],["9",["c","2"]],["9",["d","6"]]]
a.group_by(&:first).transform_values
# => "9"=>"b"=>"8", "c"=>"2", "d"=>"6"
a.group_by(&:first).transform_values.map k => v
# => ["9"=>"b"=>"8", "c"=>"2", "d"=>"6"]
edited Mar 22 at 9:45
answered Mar 22 at 9:35
sawasawa
134k31210309
134k31210309
add a comment |
add a comment |
Looking for something like this?:
some_array = [["9",["b","8"]], ["9",["c","2"]], ["9",["d","6"]]]
some_hash = some_array.each_with_object(Hash.new ) do |(k, (sub_key, sub_val)), hash|
hash[k][sub_key] = sub_val
end
p some_hash
#=> "9"=>"b"=>"8", "c"=>"2", "d"=>"6"
add a comment |
Looking for something like this?:
some_array = [["9",["b","8"]], ["9",["c","2"]], ["9",["d","6"]]]
some_hash = some_array.each_with_object(Hash.new ) do |(k, (sub_key, sub_val)), hash|
hash[k][sub_key] = sub_val
end
p some_hash
#=> "9"=>"b"=>"8", "c"=>"2", "d"=>"6"
add a comment |
Looking for something like this?:
some_array = [["9",["b","8"]], ["9",["c","2"]], ["9",["d","6"]]]
some_hash = some_array.each_with_object(Hash.new ) do |(k, (sub_key, sub_val)), hash|
hash[k][sub_key] = sub_val
end
p some_hash
#=> "9"=>"b"=>"8", "c"=>"2", "d"=>"6"
Looking for something like this?:
some_array = [["9",["b","8"]], ["9",["c","2"]], ["9",["d","6"]]]
some_hash = some_array.each_with_object(Hash.new ) do |(k, (sub_key, sub_val)), hash|
hash[k][sub_key] = sub_val
end
p some_hash
#=> "9"=>"b"=>"8", "c"=>"2", "d"=>"6"
answered Mar 22 at 9:40
SuryaSurya
13.2k23558
13.2k23558
add a comment |
add a comment |
After the question was updated:
q = [["99","99","99"],["9"]]
o = [["b","1"],["c","3"],["d","1"],["c","30"]]
q.flatten.map.with_index.with_object(Hash.new ) .transform_values(&:to_h)
#=> "99"=>"b"=>"1", "c"=>"3", "d"=>"1", "9"=>"c"=>"30"
I have update my question, for full output. Thank @iGian :)
– Tho Nguyen
Mar 23 at 11:13
[["100",["b","5"]],["101",["d","5"]],["102",["b","5"]],["103",["b","5"]],["104",["b","5"]],["105",["c","5"]],["106",["a","4"]],["106",["b","1"]],["107",["a","5"]],["108",["a","1"]],["108",["b","3"]],["108",["c","1"]],["109",["a","2"]],["109",["b","2"]],["109",["d","1"]]] I have cut output for you, I think we need edit q to another difference with first
– Tho Nguyen
Mar 23 at 11:28
@ThoNguyen, please, check my edit. I started from the original arrays.
– iGian
Mar 23 at 11:35
Thank you, I think we can make it to an object is good for reusable :)
– Tho Nguyen
Mar 23 at 11:35
Sure, your solution can solved my problem :)
– Tho Nguyen
Mar 23 at 11:37
|
show 2 more comments
After the question was updated:
q = [["99","99","99"],["9"]]
o = [["b","1"],["c","3"],["d","1"],["c","30"]]
q.flatten.map.with_index.with_object(Hash.new ) .transform_values(&:to_h)
#=> "99"=>"b"=>"1", "c"=>"3", "d"=>"1", "9"=>"c"=>"30"
I have update my question, for full output. Thank @iGian :)
– Tho Nguyen
Mar 23 at 11:13
[["100",["b","5"]],["101",["d","5"]],["102",["b","5"]],["103",["b","5"]],["104",["b","5"]],["105",["c","5"]],["106",["a","4"]],["106",["b","1"]],["107",["a","5"]],["108",["a","1"]],["108",["b","3"]],["108",["c","1"]],["109",["a","2"]],["109",["b","2"]],["109",["d","1"]]] I have cut output for you, I think we need edit q to another difference with first
– Tho Nguyen
Mar 23 at 11:28
@ThoNguyen, please, check my edit. I started from the original arrays.
– iGian
Mar 23 at 11:35
Thank you, I think we can make it to an object is good for reusable :)
– Tho Nguyen
Mar 23 at 11:35
Sure, your solution can solved my problem :)
– Tho Nguyen
Mar 23 at 11:37
|
show 2 more comments
After the question was updated:
q = [["99","99","99"],["9"]]
o = [["b","1"],["c","3"],["d","1"],["c","30"]]
q.flatten.map.with_index.with_object(Hash.new ) .transform_values(&:to_h)
#=> "99"=>"b"=>"1", "c"=>"3", "d"=>"1", "9"=>"c"=>"30"
After the question was updated:
q = [["99","99","99"],["9"]]
o = [["b","1"],["c","3"],["d","1"],["c","30"]]
q.flatten.map.with_index.with_object(Hash.new ) .transform_values(&:to_h)
#=> "99"=>"b"=>"1", "c"=>"3", "d"=>"1", "9"=>"c"=>"30"
edited Mar 23 at 11:38
answered Mar 22 at 14:43
iGianiGian
5,4902725
5,4902725
I have update my question, for full output. Thank @iGian :)
– Tho Nguyen
Mar 23 at 11:13
[["100",["b","5"]],["101",["d","5"]],["102",["b","5"]],["103",["b","5"]],["104",["b","5"]],["105",["c","5"]],["106",["a","4"]],["106",["b","1"]],["107",["a","5"]],["108",["a","1"]],["108",["b","3"]],["108",["c","1"]],["109",["a","2"]],["109",["b","2"]],["109",["d","1"]]] I have cut output for you, I think we need edit q to another difference with first
– Tho Nguyen
Mar 23 at 11:28
@ThoNguyen, please, check my edit. I started from the original arrays.
– iGian
Mar 23 at 11:35
Thank you, I think we can make it to an object is good for reusable :)
– Tho Nguyen
Mar 23 at 11:35
Sure, your solution can solved my problem :)
– Tho Nguyen
Mar 23 at 11:37
|
show 2 more comments
I have update my question, for full output. Thank @iGian :)
– Tho Nguyen
Mar 23 at 11:13
[["100",["b","5"]],["101",["d","5"]],["102",["b","5"]],["103",["b","5"]],["104",["b","5"]],["105",["c","5"]],["106",["a","4"]],["106",["b","1"]],["107",["a","5"]],["108",["a","1"]],["108",["b","3"]],["108",["c","1"]],["109",["a","2"]],["109",["b","2"]],["109",["d","1"]]] I have cut output for you, I think we need edit q to another difference with first
– Tho Nguyen
Mar 23 at 11:28
@ThoNguyen, please, check my edit. I started from the original arrays.
– iGian
Mar 23 at 11:35
Thank you, I think we can make it to an object is good for reusable :)
– Tho Nguyen
Mar 23 at 11:35
Sure, your solution can solved my problem :)
– Tho Nguyen
Mar 23 at 11:37
I have update my question, for full output. Thank @iGian :)
– Tho Nguyen
Mar 23 at 11:13
I have update my question, for full output. Thank @iGian :)
– Tho Nguyen
Mar 23 at 11:13
[["100",["b","5"]],["101",["d","5"]],["102",["b","5"]],["103",["b","5"]],["104",["b","5"]],["105",["c","5"]],["106",["a","4"]],["106",["b","1"]],["107",["a","5"]],["108",["a","1"]],["108",["b","3"]],["108",["c","1"]],["109",["a","2"]],["109",["b","2"]],["109",["d","1"]]] I have cut output for you, I think we need edit q to another difference with first
– Tho Nguyen
Mar 23 at 11:28
[["100",["b","5"]],["101",["d","5"]],["102",["b","5"]],["103",["b","5"]],["104",["b","5"]],["105",["c","5"]],["106",["a","4"]],["106",["b","1"]],["107",["a","5"]],["108",["a","1"]],["108",["b","3"]],["108",["c","1"]],["109",["a","2"]],["109",["b","2"]],["109",["d","1"]]] I have cut output for you, I think we need edit q to another difference with first
– Tho Nguyen
Mar 23 at 11:28
@ThoNguyen, please, check my edit. I started from the original arrays.
– iGian
Mar 23 at 11:35
@ThoNguyen, please, check my edit. I started from the original arrays.
– iGian
Mar 23 at 11:35
Thank you, I think we can make it to an object is good for reusable :)
– Tho Nguyen
Mar 23 at 11:35
Thank you, I think we can make it to an object is good for reusable :)
– Tho Nguyen
Mar 23 at 11:35
Sure, your solution can solved my problem :)
– Tho Nguyen
Mar 23 at 11:37
Sure, your solution can solved my problem :)
– Tho Nguyen
Mar 23 at 11:37
|
show 2 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.
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%2f55296417%2fruby-best-way-to-map-objects-into-one-array-with-fixed-key%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
1
Your "some array" is invalid.
– sawa
Mar 22 at 9:25
yes, I'm still fresher, so I don't know to call this, but I print with this output and I try to beautify this output :(
– Tho Nguyen
Mar 22 at 9:29
No, it is not reproducible. Your initial array is invalid.
– sawa
Mar 22 at 9:34
I have updated my question, for more my flow to do that output. Can you teach me how to make it smarter :(
– Tho Nguyen
Mar 22 at 9:39
3
Where does that
"10"come from in the expected output?– Stefan
Mar 22 at 9:42