by __ryan__ · 4y ago · 0 comments
(defn foo [{:keys [a b c] :as bar}]
[bar a b c])
(foo {:a 1 :b 2 :c 3}) ;; => [{:a 1, :b 2, :c 3} 1 2 3]
in JS you can do const f = ({a,b,c}) => [???, a, b, c]
but how do you bind to the whole object as well? (defn foo [{a 1
b 2 :as bar}]
[bar a b])
(foo {1 "hi" 2 "world" 3 "!"}) ;; => [{1 "hi", 2 "world", 3 "!"} "hi" "world"]
In JS of course you can have Maps with non-string keys, but destructuring doesn't work on them. Plus you have other issues like arrays in JS having equality defined by reference rather than value, making maps less useful overall. let foo = (bar) => {
let {a, b, c} = bar
return [bar, a, b, c]
}
foo({ a: 1, b: 2, c: 3 }) // => [ { a: 1, b: 2, c: 3 }, 1, 2, 3 ]
Though JavaScript object keys are strictly strings and symbols, you can destructure dynamic keys like so: let foo = (bar) => {
let {[1]: a, [2]: b} = bar
return [bar, a, b]
}
foo({ 1: "hi", 2: "world", 3: "!" }) // => [ { '1': 'hi', , '2': 'world', '3': '!' }, 'hi' 'world' ]