sick-sad-world
2/21/2017 - 7:55 AM

Update Array with value provided. Remove if exists - add if not

Update Array with value provided. Remove if exists - add if not

export const updateArrayWithValue = (arr, val) => {
  let result = [];
  let inArray = false;

  arr.forEach((item, i) => {
    if (item === val) {
      inArray = true;
    } else {
      result.push(item);
    }
  });

  if (!inArray) {
    result.push(val);
  }

  return result;
}