1

I have object. I want to add properties but I want to compute the suffix for all properties in my object during definition.

var myObject = {
    foo: "bar",
    [ "prop_" + "Access foo property: foo" ]: 42
};

Below is my expected output:

{
    foo: "bar",
    prop_bar: 42
}

It's not the case that i'm unable to achieve it. i can able to achieve by the below snippet and its working fine but i want this to be done during declaration.

let myObject = {
    foo: "bar"
};
myObject[ "prop_" + myObject['foo'] ] = 'hello'

Note to Reviewers: I have already reviewed the below questions.

  1. Is there a shorthand for this in ES6/ES7?
  2. ES6 Object Literal Property Value Shorthand
  3. How to use ES6 computed property names in node / iojs?
  4. ES6 Computed (dynamic) property names

I feel that there will be better solution than above approach, below are my questions.

What is best approach for this scenario?

How many ways we can achieve this ?

Its not possible during declaration ?

Community
  • 1
  • 1
Venkat.R
  • 7,420
  • 5
  • 42
  • 63
  • Declare `foo` outside of `myObject`. What's the use-case here? – CodingIntrigue Jan 14 '16 at 11:53
  • 1
    Seems like a duplicate of [Self-references in object literal declarations](http://stackoverflow.com/q/4616202/218196) – Felix Kling Jan 14 '16 at 13:14
  • @FelixKling, Your question mentioned is talking about the property value. i'm talking about property key computation. please check the question – Venkat.R Jan 14 '16 at 13:27
  • 1
    That doesn't make a difference. The property key and the property value are computed at the same time. – Felix Kling Jan 14 '16 at 13:27
  • I'm talking about property key computation http://es6-features.org/#ComputedPropertyNames – Venkat.R Jan 14 '16 at 13:28
  • I know. As I said, they are computed at the same time. You are trying to access the object during initialization, but it doesn't exist yet during initialization. It's impossible to access it until afterwards. There is no magic syntax to refer to other properties within an object literal. – Felix Kling Jan 14 '16 at 13:31
  • @FelixKling, I'm not expecting any magic or illusion here. i'm asking best approach, how many ways like what one answer below, final point you answered as its not possible during declaration if i'm not wrong. your given question is not ES6. its mainly about ES6 computation solution. i don't know what else to explain for you to understand. let me know still if you are not clear. Thanks BTW for your quick comment replies – Venkat.R Jan 14 '16 at 13:36
  • 1
    The best approach is to assign the other properties after the object was created, assuming there can be an arbitrary number of properties. There is no specific ES6 solution to that problem, which is why you should have a look at the question I mentioned. There isn't always a different way to do something in ES6. – Felix Kling Jan 14 '16 at 13:42
  • if you are very sure. why can't answer the question in detail rather commenting – Venkat.R Jan 14 '16 at 14:00

1 Answers1

1

You can create factory function to do this, e.g.:

const create = v => ({
    foo: v,
    ["prop_" + v]: 42
});
let myObject = create("bar");

or inline:

let myObject = (v => ({
    foo: v,
    ["prop_" + v]: 42
}))("bar");
madox2
  • 49,493
  • 17
  • 99
  • 99