mal*_*ala 5 javascript recursion object lodash
我有一个这样的对象(snake_case 中的字段)
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"
}
}
}
}
};
Run Code Online (Sandbox Code Playgroud)
我想递归地将其字段转换为camelCase,因此下面给出了预期的输出
const obj = {
vtCoreRandom: {
userDetails: {
firstName: "xyz",
lastName: "abc",
groups: [
{
id: 1,
groupType: "EXT"
},
{
id: 2,
groupType: "INT"
}
],
addressType: {
cityName: "LMN",
state: {
codeName: "KOP",
name: "PSQ"
}
}
}
}
};
Run Code Online (Sandbox Code Playgroud)
我尝试使用 mapKeys() 但我就是无法理解它的递归部分。任何帮助都受到高度赞赏。lodash如果它使过程更简单,我也有能力使用
您可以使用 lodash's_.transform()创建一个递归函数,该函数迭代键并将它们转换为驼峰式大小写_.camelCase()。Transform 也可以处理数组,是迭代对象(target)是数组,我们不需要更改键。
const camelize = obj => _.transform(obj, (acc, value, key, target) => {
const camelKey = _.isArray(target) ? key : _.camelCase(key);
acc[camelKey] = _.isObject(value) ? camelize(value) : value;
});
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 result = camelize(obj);
console.log(result);Run Code Online (Sandbox Code Playgroud)
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.15/lodash.js"></script>Run Code Online (Sandbox Code Playgroud)
(适用于 Snake_case 和 Kebap-case)
const recursiveToCamel = item => {
if (Array.isArray(item)) {
return item.map(el => recursiveToCamel(el));
} else if (typeof item === 'function' || item !== Object(item)) {
return item;
}
return Object.fromEntries(
Object.entries(item).map(([key, value]) => [
key.replace(/([-_][a-z])/gi, c => c.toUpperCase().replace(/[-_]/g, '')),
recursiveToCamel(value),
]),
);
};
Run Code Online (Sandbox Code Playgroud)
这是使用问题提供的对象的一个小演示:
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 recursiveToCamel = item => {
if (Array.isArray(item)) {
return item.map(el => recursiveToCamel(el));
} else if (typeof item === 'function' || item !== Object(item)) {
return item;
}
return Object.fromEntries(
Object.entries(item).map(([key, value]) => [
key.replace(/([-_][a-z])/gi, c => c.toUpperCase().replace(/[-_]/g, '')),
recursiveToCamel(value),
]),
);
};
console.log(recursiveToCamel(obj));Run Code Online (Sandbox Code Playgroud)
const recursiveToCamel = (item: unknown): unknown => {
if (Array.isArray(item)) {
return item.map((el: unknown) => recursiveToCamel(el));
} else if (typeof item === 'function' || item !== Object(item)) {
return item;
}
return Object.fromEntries(
Object.entries(item as Record<string, unknown>).map(
([key, value]: [string, unknown]) => [
key.replace(/([-_][a-z])/gi, c => c.toUpperCase().replace(/[-_]/g, '')),
recursiveToCamel(value),
],
),
);
};
Run Code Online (Sandbox Code Playgroud)
对于所有使用 lodash 的人,我已经操纵了可接受的答案,因为 lodash 已经包含了实用功能,例如isArray isObject camelCase
所以代码简化为这样。:)
function keysToCamel(obj) {
if (isPlainObject(obj)) {
const n = {};
Object.keys(obj).forEach(k => (n[camelCase(k)] = keysToCamel(obj[k])));
return n;
} else if (isArray(obj)) obj.map(i => keysToCamel(i));
return obj;
}
Run Code Online (Sandbox Code Playgroud)
你可以尝试这样的事情:
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 Online (Sandbox Code Playgroud)
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"
}
}
}
}
};
function toCamel(o) {
var newO, origKey, newKey, value
if (o instanceof Array) {
return o.map(function(value) {
if (typeof value === "object") {
value = toCamel(value)
}
return value
})
} else {
newO = {}
for (origKey in o) {
if (o.hasOwnProperty(origKey)) {
newKey = _.camelCase(origKey)
value = o[origKey]
if (value instanceof Array || (value !== null && value.constructor === Object)) {
value = toCamel(value)
}
newO[newKey] = value
}
}
}
return newO
}
console.log(toCamel(obj)); 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"
}
}
}
}
};
function toCamel(o) {
var newO, origKey, newKey, value
if (o instanceof Array) {
return o.map(function(value) {
if (typeof value === "object") {
value = toCamel(value)
}
return value
})
} else {
newO = {}
for (origKey in o) {
if (o.hasOwnProperty(origKey)) {
newKey = _.camelCase(origKey)
value = o[origKey]
if (value instanceof Array || (value !== null && value.constructor === Object)) {
value = toCamel(value)
}
newO[newKey] = value
}
}
}
return newO
}
console.log(toCamel(obj));
Run Code Online (Sandbox Code Playgroud)
我在这段代码中使用了lodash。