Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

第180题(2020-03-26):实现一个方法,将传入对象的下划线命名方式全部换为驼峰式(考虑递归的场景)。比如 #182

Open
qappleh opened this issue Mar 26, 2020 · 1 comment

Comments

@qappleh
Copy link
Owner

qappleh commented Mar 26, 2020

// before
const obj = {
  first_name: 'chen'
}

// after
const obj = {
  firstName: 'chen'
}
@Leo-wwj
Copy link

Leo-wwj commented May 3, 2020

const replaceUpper = (val, char='_') => {
var arr = val.split('');
var index = arr.indexOf(char)
arr.splice(index, 2, arr[index+1].toUpperCase())
var newVal = arr.join('')
return newVal
}

const filterUnderLine = (obj, char='_') => {
var arr = Object.keys(obj).filter(item => item.indexOf(char) !== -1);
arr.forEach(item => {
var before = typeof obj[item] === 'object'? filterUnderLine(obj[item]) : obj[item];
var key = replaceUpper(item)
obj[key] = before;
delete obj[item]
})
return obj
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants