0

I have an object like:

{ 
  home: 78,
  draw: 81,
  away: 36,
  u0_5: 16,
  o0_5: 26,
  u1_5: 10,
  o1_5: 68,
  u2_5: 48,
  o2_5: 50,
  u3_5: 29,
  o3_5: 31,
  u4_5: 50,
  o4_5: 5,
  u5_5: 68,
  o5_5: 56
}

my goal is to get new same structure object, but with each value multiplied by 100 and get the same structure.

 { 
   home: 7800,
   draw: 8100,
   away: 3600,
   ...
  }

3 Answers 3

2

You can use mapValues() to solve this problem, together with a callback to transform each value.

const result = _.mapValues(data, v => v * 100);

const data = { 
  home: 78,
  draw: 81,
  away: 36,
  u0_5: 16,
  o0_5: 26,
  u1_5: 10,
  o1_5: 68,
  u2_5: 48,
  o2_5: 50,
  u3_5: 29,
  o3_5: 31,
  u4_5: 50,
  o4_5: 5,
  u5_5: 68,
  o5_5: 56
};

const result = _.mapValues(data, v => v * 100);

console.log(result);
.as-console-wrapper{min-height:100%;top:0!important}
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.min.js"></script>

Sign up to request clarification or add additional context in comments.

Comments

1

With lodash/fp you can create a function with _.mapValues() and _.multiply() that iterates the object, and multiplies each value by 100:

const fn = _.mapValues(_.multiply(100))

const obj = {"home":78,"draw":81,"away":36,"u0_5":16,"o0_5":26,"u1_5":10,"o1_5":68,"u2_5":48,"o2_5":50,"u3_5":29,"o3_5":31,"u4_5":50,"o4_5":5,"u5_5":68,"o5_5":56}

const result = fn(obj)

console.log(result)
<script src='https://cdn.jsdelivr.net/g/lodash@4(lodash.min.js+lodash.fp.min.js)'></script>

Comments

0
_.forEach(data, function(value, key) {
       data.key = value*100;
});

1 Comment

While this code may answer the question, providing additional context regarding how and why it solves the problem would improve the answer's long-term value.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.