Задать вопрос
@hamster141095

Как преобразовать имена свойств объекта из kebab-case в camelCase?

Мне нужно переписать свою старую функцию согласно императивного подхода. Смысл функции - при получении данных с сервера все свойства объекта (как и вложенные) переименовывает из ке_баб в camelCase. Пример старой функции, все работало:
export const convertKeysToCamelCaseOrRemove = (
  responseData: { [key: string]: any },
  except?: Array<string>
) => {

      
  const keys =  Object.keys(responseData);
  keys.forEach((key) => {
    if (typeof responseData[key] === 'object' ) {
      convertKeysToCamelCaseOrRemove(responseData[key]);
    }
    const oldKeyArray: Array<string> | string = key.replace('__', '_').split('_');

    let newKeyArray: Array<string> = [];

    if (oldKeyArray.length === 1) {
      newKeyArray.push(oldKeyArray.toString());
    }
    const firstKeyWord: Array<string> = [];
    const restKeyWord: Array<string> = [];

    oldKeyArray.map((oldKey: string) => {
      if (oldKeyArray.indexOf(oldKey) === 0) {
        firstKeyWord.push(oldKey);
        return firstKeyWord;
      }
      const newKey = oldKey[0].toUpperCase() + oldKey.slice(1);
      restKeyWord.push(newKey);
      return restKeyWord;
    });

    newKeyArray = [...firstKeyWord, ...restKeyWord];
    const newKey = newKeyArray.join('');

    if (newKey !== key) {
      delete Object.assign(responseData, { [newKey]: responseData[key] })[key];
    }
    except &&
      except.forEach((exceptKey) => {
        if (newKey === exceptKey) {
          delete Object.assign(responseData)[exceptKey];
        }
      });



    return newKeyArray;
  });
  return responseData;
};

И новая:

export const convertKeysToCamelCaseOrRemove = (
  responseData: { [key: string]: any },
) => {

  const newArr = responseData &&  Object.entries(responseData).map((item) => {
    const key = item[0];
    const  value = item[1];


    if (typeof value === 'object' &&  value !== null &&  value !== undefined && value.length ) {
      value.map((item123:any) =>convertKeysToCamelCaseOrRemove(item123));
    }

    let arrKey =  key.replace('__', '_').split('_');
    if (arrKey.length > 1) {
      const first = arrKey[0];
      const old = arrKey.splice(1).map((oldKey => oldKey.charAt(0).toUpperCase() + oldKey.substring(1)));
      arrKey = [first, ...old];
    }
    return [arrKey.join(''), value];
  });


  return  Object.fromEntries(newArr);
  
};

Я как то неправильно делаю рекурсию.
  • Вопрос задан
  • 467 просмотров
Подписаться 1 Простой 1 комментарий
Ответ пользователя hamster141095 К ответам на вопрос (3)
@hamster141095 Автор вопроса
Изменил так, не работает при некоторых случаях
if (typeof value === 'object' &&  value !== null &&  value !== undefined && value.length ) {
      value = [...value.map((item1:any) => convertKeysToCamelCaseOrRemove(item1)
      )];
    }
Ответ написан
Комментировать