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. 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?
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. 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?
- Why do you want to use lodash to do this? This can be done with just JS – Brr Switch Commented Sep 21, 2017 at 20:20
- @VamshiGudipati - It doesn't "have" to be I guess, I just prefer the one-liners I can get out of using it. – SBB Commented Sep 21, 2017 at 20:21
2 Answers
Reset to default 12If you want to mutate the original array, use 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
:
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- to loop through every object
update
: https://lodash.com/docs/#update- to update the value
parseInt
: https://lodash.com/docs/#parseInt- in your case to update the
RuleDetailID
attribute to a number instead of a string.
- in your case to update the
example on 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..)
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"
}];