[英]lodash convert object value(string) to number
I have a very simple object like below.我有一个非常简单的对象,如下所示。 I am trying to use
lodash
as I am using it in other parts of my application.我正在尝试使用
lodash
因为我在应用程序的其他部分使用它。 I am trying to find a way to convert the value of a specific key to a number.我试图找到一种将特定键的值转换为数字的方法。
In the example below, something like:在下面的示例中,类似于:
_.mapValues(obj.RuleDetailID, _.method('parseInt'))
Object:对象:
var obj = [{
"RuleDetailID": "11624",
"AttributeValue": "172",
"Value": "Account Manager",
"IsValueRetired": "0"
}, {
"RuleDetailID": "11626",
"AttributeValue": "686",
"Value": "Agent",
"IsValueRetired": "0"
}, {
"RuleDetailID": "11625",
"AttributeValue": "180",
"Value": "Analyst",
"IsValueRetired": "0"
}, {
"RuleDetailID": "11629",
"AttributeValue": "807",
"Value": "Individual Contributor",
"IsValueRetired": "0"
}, {
"RuleDetailID": "11627",
"AttributeValue": "690",
"Value": "Senior Agent",
"IsValueRetired": "0"
}];
Expected Output:预期输出:
var obj = [{
"RuleDetailID": 11624,
"AttributeValue": "172",
"Value": "Account Manager",
"IsValueRetired": "0"
}, {
"RuleDetailID": 11626,
"AttributeValue": "686",
"Value": "Agent",
"IsValueRetired": "0"
}, {
"RuleDetailID": 11625,
"AttributeValue": "180",
"Value": "Analyst",
"IsValueRetired": "0"
}, {
"RuleDetailID": 11629,
"AttributeValue": "807",
"Value": "Individual Contributor",
"IsValueRetired": "0"
}, {
"RuleDetailID": 11627,
"AttributeValue": "690",
"Value": "Senior Agent",
"IsValueRetired": "0"
}];
Is there a chain of methods I can whip together with lodash
to achieve this?是否有一系列方法可以与
lodash
一起使用来实现这一目标?
If you want to mutate the original array, use lodash#each
:如果要改变原始数组,请使用
lodash#each
:
_.each(obj, item => item.RuleDetailID = parseInt(item.RuleDetailID, 10));
If you want to create a new array (don't mutate the original array), use lodash#map
with lodash#clone
:如果要创建新数组(不要改变原始数组),请使用
lodash#map
和lodash#clone
:
let newArr = _.map(obj, item => {
let newItem = _.clone(item);
newItem.RuleDetailID = parseInt(newItem.RuleDetailID, 10);
return newItem;
});
for mutating a single attribute of an object in an array i recommend the following:为了改变数组中对象的单个属性,我建议如下:
each
: https://lodash.com/docs/#forEach each
: https : //lodash.com/docs/#forEach
update
: https://lodash.com/docs/#update update
: https : //lodash.com/docs/#update
parseInt
: https://lodash.com/docs/#parseInt parseInt
: https://lodash.com/docs/#parseInt
RuleDetailID
attribute to a number instead of a string.RuleDetailID
属性更新为数字而不是字符串。 example on repl.it: https://repl.it/repls/AdvancedOilyParentheses#index.js repl.it 上的示例: https ://repl.it/repls/AdvancedOilyParentheses#index.js
example:例子:
_.each(arr, obj => _.update(obj, 'RuleDetailID', _.parseInt));
You used data: (FYI: I renamed obj
to arr
because is is not an object..)您使用了数据:(仅供参考:我将
obj
重命名为arr
因为它不是对象..)
var arr = [{
"RuleDetailID": "11624",
"AttributeValue": "172",
"Value": "Account Manager",
"IsValueRetired": "0"
}, {
"RuleDetailID": "11626",
"AttributeValue": "686",
"Value": "Agent",
"IsValueRetired": "0"
}, {
"RuleDetailID": "11625",
"AttributeValue": "180",
"Value": "Analyst",
"IsValueRetired": "0"
}, {
"RuleDetailID": "11629",
"AttributeValue": "807",
"Value": "Individual Contributor",
"IsValueRetired": "0"
}, {
"RuleDetailID": "11627",
"AttributeValue": "690",
"Value": "Senior Agent",
"IsValueRetired": "0"
}];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.