15 Matching Annotations
- Nov 2022
- Oct 2022
-
stedolan.github.io stedolan.github.io
-
Multiplying two objects will merge them recursively: this works like addition but if both objects contain a value for the same key, and the values are objects, the two are merged with the same strategy.
Unfortunately, it doesn't merge/concatenate arrays. Sometimes that's what you want (you want the 2nd value to override the 1st but sometimes not.
If you want it to concatenate instead, here are some workarounds:
-
https://stackoverflow.com/questions/53661930/jq-recursively-merge-objects-and-concatenate-arrays
-
If you only need/want to concatenate for some fixed list of keys, you could do it more simply like this (but could get repetitive to repeat for each key you want it for):
⟫ jq -n '[{hosts: ["a"]}, {hosts: ["b"]}] | .[]' | jq -s '.[0] * .[1] * {hosts: (.[0].hosts + .[1].hosts)}' { "hosts": [ "a", "b" ] }
-
-
- Jun 2022
- Mar 2021
-
bugs.ruby-lang.org bugs.ruby-lang.org
-
A one-liner alternative for hash-only cases can be implemented using Enumerable#reduce: root = {} [:a, :b, :c].reduce(root){@1[@2]||={}}[:d] = 'E' # root => {:a=>{:b=>{:c=>{:d=>"E"}}}}
-
I think the issues/problems specified in the comments are not present with a Hash-only implementation. :) I would be supportive of re-considering this feature just for use with a Hash, where I believe 80% of the real-life use cases would (and do) exist. I have encountered this need before in the wild, but not with Arrays.
-
Would it be desirable to specify the new object in a block? That would make it somewhat symmetrical to how Hash.new takes a block as a default value.
Tags
- I agree
- solving/handling specific case instead of the general case because general solution would be too hard/complex
- solving/handling the general case
- can provide a lambda/block to customize
- programming languages: comparison within
- one-liners
- Ruby: core
- flexible: can provide a function that can do whatever you want
- objects/hashes
- can't support everything / all cases
- simple solution
Annotators
URL
-
-
stackoverflow.com stackoverflow.com
-
// A general key transform method. Pass it a function that accepts the old key and returns // the new key. // // @example // obj = transformKeys(obj, (key) => ( // key.replace(/\b(big)\b/g, 'little') // )) export function transformKeys(source, f) { return Object.entries(source).reduce((o, [key, value]) => { o[f(key) || key] = value return o }, {}) } // Provide an object that maps from old key to new key export function rekeyObject(source, keyMap) { transformKeys(source, key => keyMap[key]) }
-
function objectMap(source,keyMap) { return Object.entries(keyMap).reduce((o,[key , newKey]) => { o[newKey]=source[key] return o;},{}) }
-
-
github.com github.com
-
Recursively transform object keys with a custom key transform strategy
-
-
github.com github.com
-
The MethodAccess extension allows you to quickly build method-based reading, writing, and querying into your Hash descendant.
-
-
trailblazer.to trailblazer.to
-
There’s no additional logic from Trailblazer happening here. The function returns a well-defined hash which is passed as an argument to step.
-
- Feb 2021
-
github.com github.com
-
For now I've worked around this by putting all the key-value pairs into an array before passing them into the interaction, then re-building the hash inside the interaction. It works, but it requires some extra code
-
-
github.com github.com
-
array :translations do hash do string :locale string :name end end array inputs can only have one input nested underneath them. This is because every element of the array must be the same type. And the inputs nested inside arrays cannot have names because they would never be used.
-