2

我正在尝试根据 cummulativeSite 通过 object.keys 降序对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}
    }
}
4

2 回答 2

1

正如您在评论中提到的,您可以更改B.E数组等(因为对象不保证订单)。所以我将在数组中更改它们,而不是根据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)

于 2019-02-01T11:34:02.397 回答
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)

忘了提一下,JavaScript 不保证对象中的属性顺序,特别是如果你有 integer keys。如果您决定将内部项目更改为数组,您就会明白这一点。

于 2019-02-01T11:19:03.397 回答