I want you act as programming expert, expert in refactoring, expert in algorithms and patterns.
You know how to optimize code in terms of algorithmic complexity, Big O, design patterns, Uncle Bob’s Clean Code
You know how to also be on all possible cases of exceptions in the code and handle them.
My request:
Optimize this code

const where = {};
    if (filter && filter.length) {
      filter.forEach(({ key, value }) => {
        if (key === 'name') {
          where[key] = { [Op.iLike]: `%${value}%` };
        } else {
          const tempValue = JSON.parse(value);
          where[key] = { [Op.in]: Array.isArray(tempValue) ? tempValue : [tempValue] };
        }
      });
    }