const removeKeyInObject = (obj, keyToRemove) => {
  return Object.keys(obj).reduce((acc, current) => {
    const value = acc[current];

    if (value === null) {
      return acc;
    }

    if (Array.isArray(value)) {
      return { ...acc, [current]: value.map(obj => removeKeyInObject(obj, keyToRemove)) };
    }

    if (typeof acc[current] === 'object') {
      return { ...acc, [current]: removeKeyInObject(value, keyToRemove) };
    }

    if (current === keyToRemove) {
      delete acc[current];
    }

    return acc;
  }, obj);
};

export default removeKeyInObject;

const removeKeyInObject = (obj, keyToRemove) => {
  if (!obj) {
    return obj;
  }

  return Object.keys(obj).reduce((acc, current) => {
    const value = acc[current];

    if (value === null) {
      return acc;
    }

    if (Array.isArray(value)) {
      return { ...acc, [current]: value.map(obj => removeKeyInObject(obj, keyToRemove)) };
    }

    if (typeof acc[current] === 'object') {
      return { ...acc, [current]: removeKeyInObject(value, keyToRemove) };
    }

    if (current === keyToRemove) {
      delete acc[current];
    }

    return acc;
  }, obj);
};

export default removeKeyInObject;

Fix null or undefined for remove key in object
Fix null or undefined for remove key in object

Signed-off-by: soupette <0a59f0508aa203bc732745954131d022d9f538a9@gmail.com>
