【问题标题】:Javascript lodash - data massaging based on attribute value [duplicate]Javascript lodash - 基于属性值的数据按摩[重复]
【发布时间】:2019-05-16 00:05:44
【问题描述】:

我有一个对象数组,例如:

var elements = [
{
    LabCode: 'VA',
    Zscore: 0.5,
    Standard: 'std1' 
},

{
    LabCode: 'RE',
    Zscore: 0.53,
    Standard: 'std1' 
},
{
    LabCode: 'VO',
    Zscore: 1.5,
    Standard: 'std1' 
},
{
    LabCode: 'VA',
    Zscore: 3.4,
    Standard: 'std2' 
},
{
    LabCode: 'RE',
    Zscore: 2.45,
    Standard: 'std2' 
},
{
    LabCode: 'VO',
    Zscore: 1.67,
    Standard: 'std2' 
}
]

我需要的是这个:

var result = [
{
Standard: 'std1',
VA: 0.5,
RE: 0.53,
VO: 1.5
},

{
Standard: 'std2',
VA: 3.4,
RE: 2.45,
VO: 1.67
}
]

Labcodes 是动态的,所以我需要能够动态地创建结果对象。我可以有很多标准,而且它们也是动态的。

我正在使用 lodash。

请注意,属性名称需要反映实验室代码也是动态的这一事实。您指出重复的答案中没有解决这个问题。

【问题讨论】:

  • 你尝试过(以 10K 的代表)......?

标签: javascript underscore.js lodash


【解决方案1】:

您可以像这样使用 reduce() 简单地做到这一点

var elements = [
                {LabCode: 'VA',Zscore: 0.5, Standard: 'std1' },
                {LabCode: 'RE',Zscore: 0.53,Standard: 'std1' },
                {LabCode: 'VO',Zscore: 1.5, Standard: 'std1' },
                {LabCode: 'VA',Zscore: 3.4, Standard: 'std2' },
                {LabCode: 'RE',Zscore: 2.45,Standard: 'std2' },
                {LabCode: 'VO',Zscore: 1.67,Standard: 'std2' }
               ]

let final = Object.values(elements.reduce((op,cur)=>{
  if( op[cur['Standard']] ) {
    op[cur['Standard']][cur['LabCode']] = cur['Zscore']
  } else {
    op[cur['Standard']] = {
     'Standard' : cur['Standard'],
     [cur['LabCode']]: cur['Zscore']
    }
  }
  return op;
}, {} ))

console.log(final);

如果是 IE,你可以使用它。

var elements = [
                    {LabCode: 'VA',Zscore: 0.5, Standard: 'std1' },
                    {LabCode: 'RE',Zscore: 0.53,Standard: 'std1' },
                    {LabCode: 'VO',Zscore: 1.5, Standard: 'std1' },
                    {LabCode: 'VA',Zscore: 3.4, Standard: 'std2' },
                    {LabCode: 'RE',Zscore: 2.45,Standard: 'std2' },
                    {LabCode: 'VO',Zscore: 1.67,Standard: 'std2' }
                   ]

    let final = elements.reduce((op,cur)=>{
      if( op[cur['Standard']] ) {
        op[cur['Standard']][cur['LabCode']] = cur['Zscore']
      } else {
        op[cur['Standard']] = {
         'Standard' : cur['Standard'],
         [cur['LabCode']]: cur['Zscore']
        }
      }
      return op;
    }, {} )
let finalop = [];
for(let key in final){
  finalop.push(final[key])
}
    console.log(finalop);

【讨论】:

  • 我不认为这个答案使用了 Lodash。
  • @sarsnake 这只是使用普通的 js。此解决方案不需要任何负载。查看演示
  • @sarsnake 你可以在这里阅读关于在 loadash stackoverflow.com/questions/35566472/…987654321@中交替的对象键
  • 对不起。 IE 不支持 Object.values stackoverflow.com/questions/42830257/…
  • @sarsnake 你应该提到这些问题本身。反正也为 IE 添加了一个版本。
【解决方案2】:

您可以通过一个 Array.reduce 和一些 ES6(不带 lodash)来完成这项工作:

var data = [{ LabCode: 'VA', Zscore: 0.5, Standard: 'std1' }, { LabCode: 'RE', Zscore: 0.53, Standard: 'std1' }, { LabCode: 'VO', Zscore: 1.5, Standard: 'std1' }, { LabCode: 'VA', Zscore: 3.4, Standard: 'std2' }, { LabCode: 'RE', Zscore: 2.45, Standard: 'std2' }, { LabCode: 'VO', Zscore: 1.67, Standard: 'std2' } ] 

const result = data.reduce((r, {LabCode, Zscore, Standard}, i, a) => {
  r[Standard] = r[Standard] || {}
  r[Standard][LabCode] = Zscore
  return i == a.length-1 ? Object.keys(r).map(k => ({Standard: k, ...r[k]})) : r
}, {})

console.log(result)

【讨论】:

    猜你喜欢
    • 2019-04-04
    • 2016-02-20
    • 2022-08-13
    • 1970-01-01
    • 2016-09-15
    • 2021-03-29
    • 1970-01-01
    • 1970-01-01
    • 2021-04-24
    相关资源
    最近更新 更多