将JS对象转换为对象数组

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

convert JS object to Object Array

问题

I have an array of objects as below:

[
  {
    Instance: 'USM 1',
    performance: '<DanPac_A>.<Daniel.DanOPCHub>.<A Session.Perfomance>',
    agc: '<DanPac_A>.<Daniel.DanOPCHub>.<A Session.AGC>',
    snr: '<DanPac_A>.<Daniel.DanOPCHub>.<A Session.SNR>',
    sos: '<DanPac_A>.<Daniel.DanOPCHub>.<A Session.SNR>',
    flowvel: '<DanPac_A>.<Daniel.DanOPCHub>.<A Session.SNR>',
  },
];

I need to take Instance out as a key and use its value as a property instead.

Expected output:

[
  {
    'USM 1': [
      {
        performance: 'value',
        agc: 'value',
        // ...
      },
    ],
  },
];

Any idea how to take it out?

英文:

I have an array of objects as below

[
  {
    Instance: &#39;USM 1&#39;,
    performance:
      &#39;&amp;lt;DanPac_A&amp;gt;.&amp;lt;Daniel.DanOPCHub&amp;gt;.&amp;lt;A Session.Perfomance&amp;gt;&#39;,
    agc: &#39;&amp;lt;DanPac_A&amp;gt;.&amp;lt;Daniel.DanOPCHub&amp;gt;.&amp;lt;A Session.AGC&amp;gt;&#39;,
    snr: &#39;&amp;lt;DanPac_A&amp;gt;.&amp;lt;Daniel.DanOPCHub&amp;gt;.&amp;lt;A Session.SNR&amp;gt;&#39;,
    sos: &#39;&amp;lt;DanPac_A&amp;gt;.&amp;lt;Daniel.DanOPCHub&amp;gt;.&amp;lt;A Session.SNR&amp;gt;&#39;,
    flowvel: &#39;&amp;lt;DanPac_A&amp;gt;.&amp;lt;Daniel.DanOPCHub&amp;gt;.&amp;lt;A Session.SNR&amp;gt;&#39;,
  },
];

I need to take Instance out as a key and use its value as a property instead.

expected output

[
  {
    &#39;USM 1&#39;: [
      {
        performance: &#39;value&#39;,
        agc: &#39;value&#39;,
        // ...
      },
    ],
  },
];

any idea how to take it out??

答案1

得分: 0

你可以使用 Array::map() 方法:

const arr = [{
    "Instance": "USM 1",
    "performance": "&lt;DanPac_A&gt;.&lt;Daniel.DanOPCHub&gt;.&lt;A Session.Perfomance&gt;",
    "agc": "&lt;DanPac_A&gt;.&lt;Daniel.DanOPCHub&gt;.&lt;A Session.AGC&gt;",
    "snr": "&lt;DanPac_A&gt;.&lt;Daniel.DanOPCHub&gt;.&lt;A Session.SNR&gt;",
    "sos": "&lt;DanPac_A&gt;.&lt;Daniel.DanOPCHub&gt;.&lt;A Session.SNR&gt;",
    "flowvel": "&lt;DanPac_A&gt;.&lt;Daniel.DanOPCHub&gt;.&lt;A Session.SNR&gt;"
}];

const mapped = arr.map(({Instance, ...item}) => ({[Instance]: [item]}));

console.log(mapped);
英文:

You could use Array::map():

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

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

const arr = [{
    &quot;Instance&quot;: &quot;USM 1&quot;,
    &quot;performance&quot;: &quot;&amp;lt;DanPac_A&amp;gt;.&amp;lt;Daniel.DanOPCHub&amp;gt;.&amp;lt;A Session.Perfomance&amp;gt;&quot;,
    &quot;agc&quot;: &quot;&amp;lt;DanPac_A&amp;gt;.&amp;lt;Daniel.DanOPCHub&amp;gt;.&amp;lt;A Session.AGC&amp;gt;&quot;,
    &quot;snr&quot;: &quot;&amp;lt;DanPac_A&amp;gt;.&amp;lt;Daniel.DanOPCHub&amp;gt;.&amp;lt;A Session.SNR&amp;gt;&quot;,
    &quot;sos&quot;: &quot;&amp;lt;DanPac_A&amp;gt;.&amp;lt;Daniel.DanOPCHub&amp;gt;.&amp;lt;A Session.SNR&amp;gt;&quot;,
    &quot;flowvel&quot;: &quot;&amp;lt;DanPac_A&amp;gt;.&amp;lt;Daniel.DanOPCHub&amp;gt;.&amp;lt;A Session.SNR&amp;gt;&quot;
}];

const mapped = arr.map(({Instance, ...item}) =&gt; ({[Instance]: [item]}));
    
console.log(mapped);

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年6月8日 06:37:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/76427504.html
匿名

发表评论

匿名网友

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

确定