0

I've seen similar questions to this one but in different languages and I am struggling to create a JavaScript equivalent.

I am receiving an object and through a function I want to change the location of one (or more) of the properties. For example,

With the original object of

{
  individual: [
    {
      dob: '2017-01-01',
      isAuthorized: true,
    },
  ],
  business: [
    {
     taxId: '123',
    },
  ],
  product: {
    code: '123',
  },
}

I would like to change the location of isAuthorized to be in the first object inside of the business array instead of individual.

Like so

{
  individual: [
    {
      dob: '2017-01-01',
    },
  ],
  business: [
    {
     taxId: '123',
     isAuthorized: true,
    },
  ],
  product: {
    code: '123',
  },
}

So far I was trying to create an object that would contain the key name and location to change it to, e.g.

{
  isAuthorized: obj.business[0]
}

And then loop over the original object as well as the object with the location values and then set the location of that key value pair.

Basically, in this function I want to see that if the original object contains a certain value (in this case isAuthorized) that it will take that key value pair and move it to the desired location.

Heretic Monkey
  • 11,687
  • 7
  • 53
  • 122
Antarktis
  • 163
  • 2
  • 10
  • 1
    "Moving" is really two steps; adding/copying the property to the new object, and removing the old property from the old object. Both of these operations have been answered before; [How to duplicate object properties in another object?](https://stackoverflow.com/q/9362716/215552) and [How do I remove a property from a JavaScript object?](https://stackoverflow.com/q/208105/215552). Perhaps you can try combining those two in your attempt and show what you've tried? – Heretic Monkey Feb 17 '20 at 21:59

1 Answers1

0

What you want can easily be achieved by using loadsh, here's a working snippet of how to restructure based on defined structure map. Extended this example to match what you want.

The example is doing a deep clone, if you are fine modifying the original object then skip that step to avoid the overhead.

// input data
const data = {
  individual: [
    {
      dob: '2017-01-01',
      isAuthorized: true,
    },
  ],
  business: [
    {
     taxId: '123',
    },
  ],
  product: {
    code: '123',
  },
};

// the structure change map
const keyMap = {
 'individual[0].isAuthorized': 'business[0].isAuthorized'
};


function parseData(data,keyMap) {
  const newData = _.cloneDeep(data);
  for( let [source,dest] of Object.entries(keyMap) ) {
    _.set(newData,dest,_.get(newData,source));
    _.unset(newData,source);
  }
  return newData;
}

console.log(parseData(data, keyMap));
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.15/lodash.min.js"></script>

Note: loadsh's set consider any numeric value as an array index so if you are using a numeric object key then use loadash.setWith. I recommend reading examples in doc for a better understanding. https://lodash.com/docs/4.17.15#set

ksankar
  • 465
  • 7
  • 13