最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - lodash convert object value(string) to number - Stack Overflow

programmeradmin1浏览0评论

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?

Share Improve this question edited Sep 21, 2017 at 20:19 SBB asked Sep 21, 2017 at 20:12 SBBSBB 8,97035 gold badges118 silver badges233 bronze badges 2
  • 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
Add a comment  | 

2 Answers 2

Reset to default 12

If 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.

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"
}];
发布评论

评论列表(0)

  1. 暂无评论