[英]Transform all keys in array from underscore to camel case in js
So, I need to transform all keys in array from underscore to camel space in js. 因此,我需要将数组中的所有键从下划线转换为js中的骆驼空间。 That is what I need to do before send form to server.
那是在将表单发送到服务器之前我需要做的。 I'm using
Angular.js
and I want to represent it as a filter (but I think it's not rly important in this case). 我正在使用
Angular.js
,我想将其表示为过滤器(但在这种情况下,我认为它并不重要)。 Anyway, here is a function I've created. 无论如何,这是我创建的功能。
.filter('underscoreToCamelKeys', function () {
return function (data) {
var tmp = [];
function keyReverse(array) {
angular.forEach(array, function (value, key) {
tmp[value] = underscoreToCamelcase(key);
});
return tmp;
}
var new_obj = {};
for (var prop in keyReverse(data)) {
if(tmp.hasOwnProperty(prop)) {
new_obj[tmp[prop]] = prop;
}
}
return new_obj;
};
function underscoreToCamelcase (string) {
return string.replace(/(\_\w)/g, function(m){
return m[1].toUpperCase();
});
}
})
Here I will try to explain how it works, because it looks terrible at first. 在这里,我将尝试解释它的工作原理,因为起初看起来很糟糕。
underscoreToCamelcase
function just reverting any string in underscore to came case, except first character (like this some_string => someString
) underscoreToCamelcase
函数只是将underscoreToCamelcase
任何字符串还原为大小写,首字符除外(例如some_string => someString
)
So, as I said earlier, I should revert all keys to camel case, but as you understant we can't simply write 因此,正如我之前说的,我应该将所有键都还原为驼峰式大小写,但是正如您所知,我们不能简单地编写
date[key] = underscoreToCamelcase(key)
so keyReverse
function returns a reverted array, here is example 所以
keyReverse
函数返回一个还原数组,这是示例
some_key
=> value
some_key
=> value
will be 将会
value
=> someKey
value
=> someKey
and for the last I simply reverting keys and values back, to get this 最后,我只是将键和值还原回去
someKey
=> value
someKey
=> value
But, as you already may understand, I got a problem, if in array exists the same values, those data will be dissapear 但是,正如您已经理解的那样,我遇到了一个问题,如果数组中存在相同的值,这些数据将消失
array 排列
some_key1
=> value
, some_key2
=> value
some_key1
=> value
, some_key2
=> value
returns as 返回为
someKey2
=> value
someKey2
=> value
So how can I fix that? 那我该如何解决呢? I have a suggestion to check if those value exists and if it is add some special substring, like this
我建议检查这些值是否存在,以及是否添加一些特殊的子字符串,如下所示
some_key1
=> value
, some_key2
=> value
some_key1
=> value
, some_key2
=> value
value
=> someKey1
, zx99value
=> someKey2
value
=> someKey1
, zx99value
=> someKey2
and after all parse it for zx99, but it I think I`m going crazy... Maybe some one have a better solution in this case? 并最终将其解析为zx99,但我想我要疯了……也许有人在这种情况下有更好的解决方案?
Important! 重要! Them main problem is not just to transform some string to camel case, but do it with array keys!
他们的主要问题不仅是将字符串转换为驼峰式大小写,还需要使用数组键!
If you use an existing library to do the camelCase transform, you can then reduce an object like so 如果使用现有库进行camelCase转换,则可以像这样减少对象
import {camelCase} from 'lodash/string'
const camelCaseKeys = (obj) =>
Object.keys(obj).reduce((ccObj, field) => ({
...ccObj,
[camelCase(field)]: obj[field]
}), {})
.filter('underscoreToCamelKeys', function () {
return function (data) {
var tmp = {};
angular.forEach(data, function (value, key) {
var tmpvalue = underscoreToCamelcase(key);
tmp[tmpvalue] = value;
});
return tmp;
};
function underscoreToCamelcase (string) {
return string.replace(/(\_\w)/g, function(m){
return m[1].toUpperCase();
});
}
})
thanks to ryanlutgen 感谢ryanlutgen
As an alternative solution, you could use the optional replacer
parameter of the JSON.stringify
method. 作为替代解决方案,您可以使用
JSON.stringify
方法的可选replacer
参数。
var result = JSON.stringify(myVal, function (key, value) {
if (value && typeof value === 'object') {
var replacement = {};
for (var k in value) {
if (Object.hasOwnProperty.call(value, k)) {
replacement[underscoreToCamelcase(k)] = value[k];
}
}
return replacement;
}
return value;
});
Of course you'll end up with a string and have to call JSON.parse
to get the object. 当然,您将得到一个字符串,并且必须调用
JSON.parse
来获取对象。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.