Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

convert snake case to camelcase javascript recursive

const obj = {
  vt_core_random: {
    user_details: {
      first_name: "xyz",
      last_name: "abc",
      groups: [
        {
          id: 1,
          group_type: "EXT"
        },
        {
          id: 2,
          group_type: "INT"
        }
      ],
      address_type: {
        city_name: "nashik",
        state: {
          code_name: "MH",
          name: "Maharashtra"
        }
      }
    }
  }
};

const toCamel = (str) => {
  return str.replace(/([-_][a-z])/ig, ($1) => {
    return $1.toUpperCase()
      .replace('-', '')
      .replace('_', '');
  });
};

const isObject = function (obj) {
  return obj === Object(obj) && !Array.isArray(obj) && typeof obj !== 'function';
};

const keysToCamel = function (obj) {
  if (isObject(obj)) {
    const n = {};

    Object.keys(obj)
      .forEach((k) => {
        n[toCamel(k)] = keysToCamel(obj[k]);
      });

    return n;
  } else if (Array.isArray(obj)) {
    return obj.map((i) => {
      return keysToCamel(i);
    });
  }
  
  return obj;
};

console.log(keysToCamel(obj));
 Run code snippetHide results
Comment

PREVIOUS NEXT
Code Example
Javascript :: angularjs $q all hash 
Javascript :: react native password qwerty 
Javascript :: toast.toastAlert ext js 
Javascript :: como contar los checkbox jquery 
Javascript :: List content on thee currentwdr 
Javascript :: moment format time 
Javascript :: javascript online string concatenation 
Javascript :: kayengxiong promise js 
Javascript :: nextjs youtube embed 
Javascript :: HSET redis, HINCRBYFLOAT redis 
Javascript :: js create an object from another object with some keys removed 
Javascript :: javascript class is not defined 
Javascript :: Priority Queue Element 
Javascript :: sort an array in descending order javascript 
Javascript :: javascript call example 
Javascript :: find leap year javascript 
Javascript :: forming a magic sqare hackerank in javascript 
Javascript :: how to get a set of values in mogodb 
Javascript :: transform js to typescript 
Javascript :: how to use handlebars.registerhelper if null 
Javascript :: how to create a new window with a specifc link jquery 
Javascript :: how to set maxLength of input type number in react 
Javascript :: absolute item onPress ToucableOpacity problem 
Javascript :: conditional ternary statement only one return 
Javascript :: getting xml from response, make sure server returns valid xml and the "content-type" header is set 
Javascript :: how to replace img url using jquery on mobile screen 
Javascript :: How to hide div based on select the dropdown in angular js 
Javascript :: Context: Cannot read properties of undefined 
Javascript :: get lat long from address google api 
Javascript :: p5 filter 
ADD CONTENT
Topic
Content
Source link
Name
8+8 =