简体   繁体   English

如何在 JavaScript 中更改对象键值对的位置

[英]How to change the location of an object key value pair in JavaScript

I've seen similar questions to this one but in different languages and I am struggling to create a JavaScript equivalent.我见过类似的问题,但使用不同的语言,我正在努力创建一个 JavaScript 等价物。

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 .我想将isAuthorized的位置更改为business数组内的第一个对象而不是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, eg到目前为止,我正在尝试创建一个包含键名和位置的对象以将其更改为,例如

{
  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.基本上,在此函数中,我想查看原始对象是否包含某个值(在本例中为isAuthorized ),它将获取该键值对并将其移动到所需位置。

What you want can easily be achieved by using loadsh, here's a working snippet of how to restructure based on defined structure map.使用 loadsh 可以轻松实现您想要的,这里是如何根据定义的结构图进行重组的工作片段。 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.注意:loadsh 的集合将任何数值视为数组索引,因此如果您使用的是数字对象键,则使用 loadash.setWith。 I recommend reading examples in doc for a better understanding.我建议阅读文档中的示例以更好地理解。 https://lodash.com/docs/4.17.15#set https://lodash.com/docs/4.17.15#set

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM