将数组中的对象及其子对象转换为映射。

huangapple go评论84阅读模式
英文:

Convert object and its sub-object in an array into map

问题

我有一个类似这样的对象数组:

let array = [
  {
    "A": [
      {
        "X": "abc123",
        "Y": "abc213",
        "Z": "xyz123"
      },
      {
        "X": "xyz7",
        "Y": "wqr",
        "Z": "dasd"
      }
    ]
  },
  {
    "B": [
      {
        "X": "sadasd",
        "Y": "asdasd",
        "Z": "sadasd"
      },
      {
        "X": "ffff531",
        "Y": "asdddz",
        "Z": "xx11155"
      }
    ]
  }
]

我想要将其转换为这样的映射,外部键仍然是A/B,但内部键应该是X的值,指向对象本身。

{
  "A": {
    "abc123": {
      "X": "abc123",
      "Y": "abc213",
      "Z": "xyz123"
    },
    "xyz7": {
      "X": "xyz7",
      "Y": "wqr",
      "Z": "dasd"
    }
  },
  "B": {
    "sadasd": {
      "X": "sadasd",
      "Y": "asdasd",
      "Z": "sadasd"
    },
    "ffff531": {
      "X": "ffff531",
      "Y": "asdddz",
      "Z": "xx11155"
    }
  }
}

我尝试使用reduce方法,但只能在没有嵌套对象时将其转换为映射,而不能处理嵌套对象。如何做到这一点?是否可以使用Array.prototype.reduce方法?

【翻译完毕】

英文:

I have an array of objects like this:

let array = [
  {
    "A": [
      {
        "X": "abc123",
        "Y": "abc213",
        "Z": "xyz123"
      },
      {
        "X": "xyz7",
        "Y": "wqr",
        "Z": "dasd"
      }
    ]
  },
  {
    "B": [
      {
        "X": "sadasd",
        "Y": "asdasd",
        "Z": "sadasd"
      },
      {
        "X": "ffff531",
        "Y": "asdddz",
        "Z": "xx11155"
      }
    ]
  }
]

And I want to convert into map such that The outer key would be A/B as it is but the inner key should be the value of X pointing towards the value as the object itself.

 {
    "A": {
      
        "abc123":{
           "X": "abc123",
           "Y": "abc213",
           "Z": "xyz123"
        
      },
        "xyz7": {
          "X": "xyz7",
          "Y": "wqr",
          "Z": "dasd"
        
      }
    
  },
    "B": {
      
        "sadasd":{
          "X": "sadasd",
          "Y": "asdasd",
          "Z": "sadasd"
         },
      
        "ffff531":{
           "X": "ffff531",
           "Y": "asdddz",
           "Z": "xx11155"
        }
      
     }
}

I tried using the reduce method but I was only able to covert this to map when there are no nested objects but not with nested objects.

How can I do this ? Is this possible using Array.prototype.reduce method?

答案1

得分: 2

你可以构建一个单一对象,并创建具有所需键的嵌套对象。

const data = [{ A: [{ X: "abc123", Y: "abc213", Z: "xyz123" }, { X: "xyz7", Y: "wqr", Z: "dasd" }] }, { B: [{ X: "sadasd", Y: "asdasd", Z: "sadasd" }, { X: "ffff531", Y: "asdddz", Z: "xx11155" }] }];
const result = Object.fromEntries(Object.entries(Object.assign({}, ...data)).map(([k, v]) => [k, Object.fromEntries(v.map(o => [o.X, o]))]));

console.log(result);
英文:

You could build a single object and create nested object with wanted key.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const
    data = [{ A: [{ X: &quot;abc123&quot;, Y: &quot;abc213&quot;, Z: &quot;xyz123&quot; }, { X: &quot;xyz7&quot;, Y: &quot;wqr&quot;, Z: &quot;dasd&quot; }] }, { B: [{ X: &quot;sadasd&quot;, Y: &quot;asdasd&quot;, Z: &quot;sadasd&quot; }, { X: &quot;ffff531&quot;, Y: &quot;asdddz&quot;, Z: &quot;xx11155&quot; }] }],
    result = Object.fromEntries(Object
        .entries(Object.assign({}, ...data))
        .map(([k, v]) =&gt; [k, Object.fromEntries(v.map(o =&gt; [o.X, o]))]));

console.log(result);

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年5月11日 02:56:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/76221763.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定