排序嵌套对象

时间:2019-02-01 11:01:27

标签: javascript jquery sorting javascript-objects nested-object

我正在尝试通过object.keys以cummulativeSite的降序对javascript中的obj对象的对象进行排序,但是没有对其进行排序。请提出一个好的方法或用于对该对象进行排序的方法。

var obj={


 B.E:{
    A: {percentgsite: 8.5, backlogenclsite: 8.5, regularenclsite: 3.5, cummulativeSite: 20.5},
    B: {percentgsite: 5.9, backlogenclsite: 5.9, regularenclsite: 8.5, cummulativeSite: 20.3},
    C: {percentgsite: 5.9, backlogenclsite: 6.4, regularenclsite: 8.9, cummulativeSite: 21.200000000000003}
    },



 Diploma:{
    A: {percentgsite: 8.5, cummulativeSite: 5.2},
    B: {percentgsite: 5.9, cummulativeSite: 8.5},
    C: {percentgsite: 5.9, cummulativeSite: 5.6}
    },



 M.E:{
    A: {percentgsite: 8.5, cummulativeSite: 11.9},
    B: {percentgsite: 5.9, cummulativeSite: 12.9},
    C: {percentgsite: 5.9, cummulativeSite: 13.5}
    }
}

预期输出:

var obj={


 B.E:{

    C: {percentgsite: 5.9, backlogenclsite: 6.4, regularenclsite: 8.9, cummulativeSite: 21.200000000000003},
    A: {percentgsite: 8.5, backlogenclsite: 8.5, regularenclsite: 3.5, cummulativeSite: 20.5},
    B: {percentgsite: 5.9, backlogenclsite: 5.9, regularenclsite: 8.5, cummulativeSite: 20.3}
    },



 Diploma:{
    B: {percentgsite: 8.5, cummulativeSite: 8.5},
    C: {percentgsite: 5.9, cummulativeSite: 5.6},
    A: {percentgsite: 5.9, cummulativeSite: 5.2}
    },



 M.E:{
    C: {percentgsite: 8.5, cummulativeSite: 13.5},
    B: {percentgsite: 5.9, cummulativeSite: 12.9},
    A: {percentgsite: 5.9, cummulativeSite: 11.9}
    }
}

2 个答案:

答案 0 :(得分:1)

正如您在注释中提到的,可以将B.E等更改为数组(因为object不能保证顺序)。因此,我将按数组更改它们,而不是根据cummulativeSite属性对其进行排序

var obj={'B.E':[{A:{percentgsite:8.5,backlogenclsite:8.5,regularenclsite:3.5,cummulativeSite:20.5}},{B:{percentgsite:5.9,backlogenclsite:5.9,regularenclsite:8.5,cummulativeSite:20.3}},{C:{percentgsite:5.9,backlogenclsite:6.4,regularenclsite:8.9,cummulativeSite:21.200000000000003}}],Diploma:[{A:{percentgsite:8.5,cummulativeSite:5.2}},{B:{percentgsite:5.9,cummulativeSite:8.5}},{C:{percentgsite:5.9,cummulativeSite:5.6}}],'M.E':[{A:{percentgsite:8.5,cummulativeSite:11.9}},{B:{percentgsite:5.9,cummulativeSite:12.9}},{C:{percentgsite:5.9,cummulativeSite:13.5}}]}

Object.keys(obj).forEach(e => {
  obj[e].sort( (a,b) =>{
   return Object.values(a)[0].cummulativeSite -
   Object.values(b)[0].cummulativeSite
   })
})

console.log(obj)

答案 1 :(得分:0)

您可以像这样使用嵌套的Object.entriesreduce

var obj={"B.E":{A:{percentgsite:8.5,backlogenclsite:8.5,regularenclsite:3.5,cummulativeSite:20.5},B:{percentgsite:5.9,backlogenclsite:5.9,regularenclsite:8.5,cummulativeSite:20.3},C:{percentgsite:5.9,backlogenclsite:6.4,regularenclsite:8.9,cummulativeSite:21.200000000000003}},"Diploma":{A:{percentgsite:8.5,cummulativeSite:5.2},B:{percentgsite:5.9,cummulativeSite:8.5},C:{percentgsite:5.9,cummulativeSite:5.6}},"M.E":{A:{percentgsite:8.5,cummulativeSite:11.9},B:{percentgsite:5.9,cummulativeSite:12.9},C:{percentgsite:5.9,cummulativeSite:13.5}}}

const sortedObj = Object.entries(obj).reduce((acc, [dept, v]) =>{
  const sortedDept = Object.entries(v)
             .sort(([k1,v1], [k2,v2]) => v2.cummulativeSite - v1.cummulativeSite)
             .reduce((r, [k,v]) => (r[k] = v, r), {});
  
  acc[dept] = sortedDept;
  return acc;
}, {})

console.log(sortedObj)

忘记提及properties order in objects is not guaranteed in JavaScript,尤其是当您使用整数keys时。如果您决定将内部项目更改为数组,您就会明白。