score:3

Accepted answer
addCounter = row => {
  const { counters } = this.state;
  const existingRow = counters.filter(c => c.id === row.id)[0];

  if (existingRow.length !== 0) {
    this.setState(
      Object.assign(existingRow, { value: existingRow.value + 1 })
    );
  } else {
    this.setState({ counters: counters.concat(row) });
  }
};

score:0

addCounter = row => {

  const { counters } = this.state;

  //map creates a brand new array
  let newCounters = counters.map((existingRow, index) => {
      if(index === row.id){
          if (existingRow.length !== 0) {
              //Using Object assign Copies all values  from existing row and the object literal to a brand new object
              //return Object.assign({}, existingRow, { value: existingRow.value + 1 });
              //Also instead use the spread operator for more clarity
              return {
                  ...existingRow,
                  value: existingRow.value+1
              };
          } else {
              return row;
          }
      }

      //Just return all other object
      return item;
  });

  this.setState({ counters : newCoutners });
}

Related Query

More Query from same tag