1

why it doesn't work.
And it shows the Error

invalid left-hand side in assignment expression

VueJs


for (let i = 1; i <= this.properties.length; i++) {

    this.product.properties + `.property${i}` = '';

}‍‍‍‍‍‍

data

data () {
  return {
     product: {
       properties:{}
     }
  }
}
1
  • you can't perform an operation (+) before the =, to access a property an object dynamically you should always write myObject[dynamicKeyString] Commented Oct 10, 2019 at 20:39

3 Answers 3

3

Try this:

this.product = {
  properties: {}
};

for (var i = 0, len = 10; i < len; i++) {
  this.product.properties["property" + i] = '';
}

console.log(this.product);

Sign up to request clarification or add additional context in comments.

Comments

1

product.properties[property${i}] = ''

Comments

0
v left-hand side                            v right-hand side
this.product.properties + `.property${i}` = '';

In Javascript, on left-hand side of assigment operator must be something to which you can assign a value e.g. variable or object property. In your case there is addition expression there which is not valid.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.