-2

I understand that in ES2015+, instead of writing:

let myObject = { a: "a", b: "b", c: "c" };

I can use object shorthand to write the following:

let a = "a";
let b = "b";
let c = "c";

let myObject = { a, b, c };
console.log(myObject);

But that doesn't resemble the shorthand I am looking for.

Is there an approach in which I can declare an object literal without first defining variables and the properties of that object literal will be automatically assigned values which are stringified versions of the object's property names?

I.e. I write something similar to this:

let myObject = { a, b, c };

and it automatically resolves as:

let myObject = { a: "a", b: "b", c: "c" };

Or is that kind of shorter shorthand simply not possible?


Some background to this question:

My use-case is accepting both values and name-value pairs from users. The latter is straightforward enough. In the case of the former, I don't wish to make the user jump through the hoop of adding a name and then an identical value where one would suffice.

19
  • 2
    simply no . . . Oct 31, 2020 at 10:41
  • 1
    Such an object wouldn't be particularly useful, because it's a mapping from a value you already have to that same value (obj["foo"] === "foo", for example, but you already had "foo"). You could make a Proxy that just returns the name of the prop you asked for, but it might help to provide some context on why you'd need this.
    – jonrsharpe
    Oct 31, 2020 at 10:48
  • 1
    Why not accept either an object or an array, then, and in the case of the array ["a", "b", "c"] it's a simple transform to the object {a: "a", b: "b", c: "c"} using e.g. .reduce.
    – jonrsharpe
    Oct 31, 2020 at 10:57
  • 1
    The addition to your question mentions user provided values. So could you add the boiler plate code for the function that would receive such input, and indicate what the output of that function would need to be? I ask, because I can't see how an object literal could have anything to do with user input... which is by definition not literal.
    – trincot
    Oct 31, 2020 at 11:06
  • 1
    Call it literal or syntax, presumably you're talking about user input in terms of someone creating an object with which to invoke part of the API you're providing (e.g. a library they're consuming). In which case, are you asking about this in terms of documenting how they might do so (do they not know how to create objects)? Or are you proposing that your function implement something that makes it possible? Or are they passing e.g. a string you're planning to parse?
    – jonrsharpe
    Oct 31, 2020 at 11:26

1 Answer 1

0

There is no syntax that would understand {a, b, c} when none of these a, b and c are defined variables. Moreover, it would not give a clue about the data types of these values, so in case of strings, you would need string values, not (undefined) variable references.

From comments, it seems you get user input in the form of a string, which would include a series of pairs and single values.

Here is a function that would parse such a string:

const toObject = str =>
    Object.fromEntries(
        str.split(" ")
           .map(pair => pair.split("^"))
           .map(([k, v]) => [k, v??k])
    );

let s = "name^John male height^185cm";
let obj = toObject(s);
console.log(obj);

Not the answer you're looking for? Browse other questions tagged or ask your own question.