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
  • 8,947
  • 21
  • 78
  • 151

2 Answers2

2
const selectedData = {};
selectedData[field] = value;
millimoose
  • 37,888
  • 9
  • 76
  • 132
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
  • 44,345
  • 11
  • 88
  • 115