2

I am trying to create an object using the field as the property and the value as the value.

  onChange(e) {
    const field = e.target.name;
    const value = e.target.value;
    const selectedData = { field, value };
    this.props.dispatch(updateForm({ selectedData }));
  }

Can anyone help?

Bomber
  • 10,195
  • 24
  • 90
  • 167

2 Answers2

2
const selectedData = {};
selectedData[field] = value;
millimoose
  • 39,073
  • 9
  • 82
  • 134
1

You can do in this using bracket notation.

 let selectedData = {[field]: value};

Short example:

let field="number";
let value=10;
const selectedData = {[field]: value};
console.log(selectedData);
Mihai Alexandru-Ionut
  • 47,092
  • 13
  • 101
  • 128