# Set multiple object values (\`a: b: undefined\`)

**URL:** <https://es.discourse.group/t/set-multiple-object-values-a-b-undefined/1052>\
**Category:** 💡 Ideas\
**Tags:** proposal\
**Created:** [October 24, 2021, 6:39am UTC](https://es.discourse.group/t/set-multiple-object-values-a-b-undefined/1052 "2021-10-24T06:39:24Z")\
**Posts on this page:** 19\
**Page:** 1

<div class="post-metadata">

**Author:** ![SiddharthShyniben](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/siddharthshyniben/32/1079_2.png) [@SiddharthShyniben](https://es.discourse.group/u/SiddharthShyniben)\
**Post date:** [October 24, 2021, 6:39am UTC](https://es.discourse.group/t/set-multiple-object-values-a-b-undefined/1052/1 "2021-10-24T06:39:24Z")

</div>

❌ Old way:

```javascript
let defaltPerson = {
  name: null,
  gender: null,
  age: null,
  country: null,
  username: getId(),
  id: getId()
}

```

❌ Chained assignments way:

```javascript
let defaltPerson = {}

defaultPerson.name = defaultPerson.gender = defaultPerson.age = defaultPerson.country = null;
defaultPerson.username = defaultPerson.id = getId();

```

❌ Hacky function way

```javascript
let person = {
  ...['name', 'gender', 'age', 'country'].reduce((obj, prop)=>({...obj, [prop]: null}), {}),
  ...['username', 'id'].reduce((obj, prop)=>({...obj, [prop]: getId()}), {}),
}

```

❌ Hacky function as array prototype way

```javascript
Array.prototype.ditto = function(v) {
  return this.reduce((o, p)=>({...o, [p]: v}), {})
}

let person = {
  ...['name', 'gender', 'age', 'country'].ditto(null),
  ...['username', 'id'].ditto(getId())
}

```

✅ New way:

```javascript
let defaltPerson = {
  name: gender: age: country: null,
  username: id: getId()
}

```

Some methods taken from [javascript - Can one set multiple properties inside an object literal to the same value? - Stack Overflow](https://stackoverflow.com/questions/13104949/can-one-set-multiple-properties-inside-an-object-literal-to-the-same-value)

---

<div class="post-metadata">

**Author:** ![lightmare](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/lightmare/32/843_2.png) [@lightmare](https://es.discourse.group/u/lightmare)\
**Post date:** [October 24, 2021, 8:16am UTC](https://es.discourse.group/t/set-multiple-object-values-a-b-undefined/1052/2 "2021-10-24T08:16:30Z")

</div>

There's a notable difference between your examples 1,3 versus 2,4,5 in how many times they call `getId()`. Of course both ways are valid, but in different scenarios ;)

---

<div class="post-metadata">

**Author:** ![jithujoshyjy](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/jithujoshyjy/32/399_2.png) [@jithujoshyjy](https://es.discourse.group/u/jithujoshyjy)\
**Post date:** [October 24, 2021, 12:12pm UTC](https://es.discourse.group/t/set-multiple-object-values-a-b-undefined/1052/3 "2021-10-24T12:12:06Z")

</div>

How about extending the pick notation?

```javascript
const obj = Object.create(null)
obj.{prop1, prop2} = null
// obj.prop1 = obj.prop2 = null

// sadly the second one doesn't exactly match your interest
obj.{prop3, prop4} = getId()
// // obj.prop3 = obj.prop4 = getId()

```

I guess that behaviour should be implemented through loops;

```javascript
for(let x of ["prop3", "prop4"])
    obj[x] = getId()

```

---

<div class="post-metadata">

**Author:** ![SiddharthShyniben](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/siddharthshyniben/32/1079_2.png) [@SiddharthShyniben](https://es.discourse.group/u/SiddharthShyniben)\
**Post date:** [October 24, 2021, 2:43pm UTC](https://es.discourse.group/t/set-multiple-object-values-a-b-undefined/1052/4 "2021-10-24T14:43:42Z")

</div>

> [@lightmare](#):
>
> There's a notable difference between your examples 1,3 versus 2,4,5 in how many times they call `getId()`. Of course both ways are valid, but in different scenarios ;)

Yes, I know that, `getId()` may return different values. That is just an implementation detail, so I left it like that.

---

<div class="post-metadata">

**Author:** ![SiddharthShyniben](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/siddharthshyniben/32/1079_2.png) [@SiddharthShyniben](https://es.discourse.group/u/SiddharthShyniben)\
**Post date:** [October 24, 2021, 2:45pm UTC](https://es.discourse.group/t/set-multiple-object-values-a-b-undefined/1052/5 "2021-10-24T14:45:02Z")

</div>

That would also be doable

> sadly the second one doesn't exactly match your interest

Like i said, that's just an implementation detail. Just assume `getId` returns the same value for now.

---

<div class="post-metadata">

**Author:** ![claudiameadows](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/claudiameadows/32/126_2.png) [@claudiameadows](https://es.discourse.group/u/claudiameadows)\
**Post date:** [October 25, 2021, 11:02pm UTC](https://es.discourse.group/t/set-multiple-object-values-a-b-undefined/1052/6 "2021-10-25T23:02:30Z")

</div>

I read that and expect the RHS should be an object. To me, `obj.{foo, bar} = value` looks like sugar for `obj.foo = value.foo; obj.bar = value.bar`.

---

<div class="post-metadata">

**Author:** ![jithujoshyjy](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/jithujoshyjy/32/399_2.png) [@jithujoshyjy](https://es.discourse.group/u/jithujoshyjy)\
**Post date:** [October 26, 2021, 3:23am UTC](https://es.discourse.group/t/set-multiple-object-values-a-b-undefined/1052/7 "2021-10-26T03:23:16Z")

</div>

source: [link](https://github.com/rtm/js-pick-notation)

---

<div class="post-metadata">

**Author:** ![rumkin](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/rumkin/32/598_2.png) [@rumkin](https://es.discourse.group/u/rumkin)\
**Post date:** [October 26, 2021, 9:09am UTC](https://es.discourse.group/t/set-multiple-object-values-a-b-undefined/1052/8 "2021-10-26T09:09:14Z")

</div>

I'd like to propose alternative syntax with another separator:

```javascript
let object = {
  x|y|z: 1,
  // or
  x/y/z: 1,
}

```

Such syntax has some advantage over colon based. It can be applied to other kinds of property definitions, not to object initializer only:

```javascript
class {
  static x/y/z = 1;
  async method/methodAlias() {}
}

```

Also I find this proposal especially useful with [Record & Tuple Proposal](https://github.com/tc39/proposal-record-tuple), IMO it will simplify usage of complex default values.

---

<div class="post-metadata">

**Author:** ![theScottyJam](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/thescottyjam/32/616_2.png) [@theScottyJam](https://es.discourse.group/u/theScottyJam)\
**Post date:** [October 26, 2021, 2:04pm UTC](https://es.discourse.group/t/set-multiple-object-values-a-b-undefined/1052/9 "2021-10-26T14:04:18Z")

</div>

@jithujoshyjy - your source link actually explains that syntax in the way @claudiameadows was expecting it to work :p

> the syntax `obj.{p1, p2} = obj2` means to extract the values of `p1` and `p2` from `obj2` and add them to `obj1` .

---

<div class="post-metadata">

**Author:** ![jithujoshyjy](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/jithujoshyjy/32/399_2.png) [@jithujoshyjy](https://es.discourse.group/u/jithujoshyjy)\
**Post date:** [October 27, 2021, 2:09am UTC](https://es.discourse.group/t/set-multiple-object-values-a-b-undefined/1052/10 "2021-10-27T02:09:54Z")

</div>

Yeah, I realised that and wanted to provide some preface ;)

---

<div class="post-metadata">

**Author:** ![DiriectorDoc](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/diriectordoc/32/987_2.png) [@DiriectorDoc](https://es.discourse.group/u/DiriectorDoc)\
**Post date:** [October 29, 2021, 9:08pm UTC](https://es.discourse.group/t/set-multiple-object-values-a-b-undefined/1052/11 "2021-10-29T21:08:49Z")

</div>

What do you think of this notation?

```javascript
let obj = {
   "hello": "world",
    ["foo", "bar"]: "baz"
}

console.log(obj.foo) // "baz"
console.log(obj.bar) // "baz"

```

---

<div class="post-metadata">

**Author:** ![ljharb](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/ljharb/32/8_2.png) [@ljharb](https://es.discourse.group/u/ljharb)\
**Post date:** [October 29, 2021, 9:22pm UTC](https://es.discourse.group/t/set-multiple-object-values-a-b-undefined/1052/12 "2021-10-29T21:22:56Z")

</div>

with parens, this already has a meaning:

```javascript
"hello": "world",
[("foo", "bar")]: "baz"
}

```

The difference between your example and this one is just one set of parens, and I think that's way too subtle to be a viable option.

---

<div class="post-metadata">

**Author:** ![theScottyJam](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/thescottyjam/32/616_2.png) [@theScottyJam](https://es.discourse.group/u/theScottyJam)\
**Post date:** [October 29, 2021, 9:30pm UTC](https://es.discourse.group/t/set-multiple-object-values-a-b-undefined/1052/13 "2021-10-29T21:30:17Z")

</div>

Without parens it's also valid syntax.

```javascript
> obj = { 'world': 2 }
> obj['hello', 'world']
2

```

**Edit:** Ignore this post. Don't know what I was thinking. We're talking about object creation, not property access.

---

<div class="post-metadata">

**Author:** ![senocular](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/senocular/32/642_2.png) [@senocular](https://es.discourse.group/u/senocular)\
**Post date:** [October 29, 2021, 10:29pm UTC](https://es.discourse.group/t/set-multiple-object-values-a-b-undefined/1052/14 "2021-10-29T22:29:28Z")

</div>

> [@theScottyJam](#):
>
> **Edit:** Ignore this post. Don't know what I was thinking. We're talking about object creation, not property access.

TIL - I fully expected it to work in computed property names as well. Seems logical given:

```js
let obj = {
   "hello": "world",
}
obj["foo", "bar"] = "baz"
obj // {hello: 'world', bar: 'baz'}

```

---

<div class="post-metadata">

**Author:** ![claudiameadows](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/claudiameadows/32/126_2.png) [@claudiameadows](https://es.discourse.group/u/claudiameadows)\
**Post date:** [November 8, 2021, 5:14am UTC](https://es.discourse.group/t/set-multiple-object-values-a-b-undefined/1052/15 "2021-11-08T05:14:32Z")

</div>

Post still holds - it's the same grammar inside the brackets. (It's part of why brackets were chosen for that syntactic form - it mirrors dynamic property access.)

---

<div class="post-metadata">

**Author:** ![theScottyJam](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/thescottyjam/32/616_2.png) [@theScottyJam](https://es.discourse.group/u/theScottyJam)\
**Post date:** [November 8, 2021, 2:28pm UTC](https://es.discourse.group/t/set-multiple-object-values-a-b-undefined/1052/16 "2021-11-08T14:28:44Z")

</div>

If you actually execute `{ ['a', 'b']: 2 }`, it throws an error at that comma. Which was very surprising to me - dunno why it disallows a comma there but allows anything else. So unless this is actually a bug in V8 and the comma operator is supposed to work there, like intuition would expect...

---

<div class="post-metadata">

**Author:** ![aclaymore](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/aclaymore/32/501_2.png) [@aclaymore](https://es.discourse.group/u/aclaymore)\
**Post date:** [November 8, 2021, 8:57pm UTC](https://es.discourse.group/t/set-multiple-object-values-a-b-undefined/1052/17 "2021-11-08T20:57:27Z")

</div>

> [@theScottyJam](#):
>
> Which was very surprising to me - dunno why it disallows a comma there but allows anything else. So unless this is actually a bug in V8 and the comma operator is supposed to work there

It is not a bug. The spec grammar is (roughly):

```javascript
{
  [AssignmentExpression]: AssignmentExpression
} 

```

And the comma operator is not an [AssignmentExpression](https://262.ecma-international.org/12.0/#prod-AssignmentExpression), but an [Expression](https://262.ecma-international.org/12.0/#prod-Expression).

EDIT: It was already like this in the first edition of the spec from 1997. [https://www.ecma-international.org/wp-content/uploads/ECMA-262\_1st\_edition\_june\_1997.pdf](https://www.ecma-international.org/wp-content/uploads/ECMA-262_1st_edition_june_1997.pdf) see section 11.14.

---

<div class="post-metadata">

**Author:** ![senocular](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/senocular/32/642_2.png) [@senocular](https://es.discourse.group/u/senocular)\
**Post date:** [November 8, 2021, 9:58pm UTC](https://es.discourse.group/t/set-multiple-object-values-a-b-undefined/1052/18 "2021-11-08T21:58:40Z")

</div>

But why is it `AssignmentExpression` and not `Expression` like bracket access?

---

<div class="post-metadata">

**Author:** ![claudiameadows](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/claudiameadows/32/126_2.png) [@claudiameadows](https://es.discourse.group/u/claudiameadows)\
**Post date:** [November 17, 2021, 8:03am UTC](https://es.discourse.group/t/set-multiple-object-values-a-b-undefined/1052/19 "2021-11-17T08:03:31Z")

</div>

The issue @senocular is pointing out isn't what `AssignmentExpression` supports, but the apparent inconsistency between object literal keys (which uses `AssignmentExpression`) and dynamic property access (which uses `Expression`). It feels like a spec oversight, so I've filed [Shouldn't `ComputedPropertyName` use `Expression` and not `AssignmentExpression`? · Issue #2581 · tc39/ecma262 · GitHub](https://github.com/tc39/ecma262/issues/2581) in response. (I'd also prefer to keep further discussion there, to avoid further driving this thread off-topic.)
