0

I'm trying to remove lodash from one of my projects, so I'm going through and substituting the lodash methods I've used with either native JS ones or just ones I have written myself. Although, I am currently stuck on lodash transform. I'm using it on an object and I'm struggling to work out how to replace it. I could achieve something similar with reduce, but reduce only works on arrays..

For example,

let obj = {
   'field1': 1,
   'field2': 2,
   'field3': 3
}

let transformed = _.transform(obj, (result, value, key) => {
   if (typeof value !== 'object') {
      result[key] = value * 2;
   }
}

Is there a method I'm overlooking... Any help would be appreciated.

SkinnyBetas
  • 461
  • 1
  • 5
  • 27
  • 3
    Looks like this can be done with [`Object.entries`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/entries), [`map`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map), [`Object.fromEntries`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/fromEntries). – Sebastian Simon Mar 22 '21 at 02:16
  • [Duplicate](https://google.com/search?q=site%3Astackoverflow.com+js+map+object+values) of [map function for objects (instead of arrays)](https://stackoverflow.com/q/14810506/4642212). – Sebastian Simon Mar 22 '21 at 02:18

0 Answers0