# String-Key-Only Object Declaration - Just like Array, but with direct access

**URL:** <https://es.discourse.group/t/string-key-only-object-declaration-just-like-array-but-with-direct-access/609>\
**Category:** 💡 Ideas\
**Created:** [January 4, 2021, 10:49pm UTC](https://es.discourse.group/t/string-key-only-object-declaration-just-like-array-but-with-direct-access/609 "2021-01-04T22:49:42Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![DenisTRUFFAUT](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/denistruffaut/32/569_2.png) [@DenisTRUFFAUT](https://es.discourse.group/u/DenisTRUFFAUT)\
**Post date:** [January 4, 2021, 10:49pm UTC](https://es.discourse.group/t/string-key-only-object-declaration-just-like-array-but-with-direct-access/609/1 "2021-01-04T22:49:43Z")

</div>

# Context

Consider the following code

```javascript
const ext = 'webp';
const o = { 'html': 1, 'css': 1, 'mjs': 1, 'js': 1, 'json': 1 };
console.log(o[ext] ? 'Found' : 'Not found'); 

```

To check if an element exists in a collection, its declaration is unecessary verbose.  
Lots of useless 1's here.

# Proposal

I suggest to be able to replace

```javascript
const o = { 'html': 1, 'css': 1, 'mjs': 1, 'js': 1, 'json': 1 };

```

by

```javascript
const o = { 'html', 'css', 'mjs', 'js', 'json' };

```

Just like an array, but with direct access.

# Q / A

Q : It has to be a valid Object. What about the value ?  
A : The implicit behavior is : Value equals Key

It means

```javascript
const o = { 'html', 'css', 'mjs', 'js', 'json' };

```

is implicitely interpreted as

```javascript
const o = { 'html':'html', 'css':'css', 'mjs':'mjs', 'js':'js', 'json':'json' };

```

--

Q : What about spread ?  
A : You can do that.

It means

```javascript
const o = { 'html', 'css', ...{ 'mjs', 'js' }, ...{ 'json' } };

```

is implicitely interpreted as

```javascript
const o = { 'html':'html', 'css':'css', ...{ 'mjs':'mjs', 'js':'js' }, ...{ 'json':'json' } };

```

--

Q : What about self-assignment ?  
A : Self-assignment is impossible, it would conflict with accessing to the property.

Ex:

```javascript
const o = Object();
o.html;
o['css'];

```

It cannot be a write operation.  
It is a read-only operation.  
Self-assignment is impossible.

--

See also : [when object declaration passed with single string, consider the same string as key == value like variable shorthand](https://es.discourse.group/t/when-object-declaration-passed-with-single-string-consider-the-same-string-as-key-value-like-variable-shorthand/249)

---

<div class="post-metadata">

**Author:** ![DenisTRUFFAUT](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/denistruffaut/32/569_2.png) [@DenisTRUFFAUT](https://es.discourse.group/u/DenisTRUFFAUT)\
**Post date:** [January 4, 2021, 11:45pm UTC](https://es.discourse.group/t/string-key-only-object-declaration-just-like-array-but-with-direct-access/609/2 "2021-01-04T23:45:40Z")

</div>

Surma says to use :

```javascript
const ext = 'webp';
const o = new Set(['html', 'css', 'mjs', 'js', 'json']);
console.log(o.has(ext) ? 'Found' : 'Not found'); 

```

> <https://twitter.com/DasSurma/status/1346238117905395712>

---

<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:** [January 5, 2021, 10:27am UTC](https://es.discourse.group/t/string-key-only-object-declaration-just-like-array-but-with-direct-access/609/3 "2021-01-05T10:27:33Z")

</div>

Is there a difference to the other thread?

> [@when object declaration passed with single string, consider the same string as key == value like variable shorthand](https://es.discourse.group/t/when-object-declaration-passed-with-single-string-consider-the-same-string-as-key-value-like-variable-shorthand/249):
>
> As currently we do this for variable. Like this const name = 'jo' const obj = {name} console.log(obj) // output =\> {name : 'jo'} Proposal So have the same behavior with string type as well. like this const obj = {'jo'} console.log(obj) // output =\> { jo : 'jo' } example 2 with spaces, it would be work like usual string with space behavior Current const name = 'name with space' const obj = {name} console.log(obj) // output =\> {'name-with-space': 'name with space'} With this proposal …

---

<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:** [February 15, 2021, 5:54pm UTC](https://es.discourse.group/t/string-key-only-object-declaration-just-like-array-but-with-direct-access/609/4 "2021-02-15T17:54:19Z")

</div>

Your proposed value is _not_ intuitive here.

Also not a fan of automatically inferring values other than variable names - it feels like it's just a very mild nice to have that isn't actually worth much.
