2017-05-25 185 views
0

我需要對嵌套對象數組進行排序。我寫了下面的代碼,但由於某些原因,它將第一個元素始終放在返回的數組中,即使它不應該。對嵌套對象排序數組

orderedArray =() => { 
    let sortedArray = products 
    if(this.state.interests.design && this.state.interests.code && this.state.interests.business) 
     sortedArray.sort((a,b)=> (b.scores.quality-a.scores.quality)) 
    else if(this.state.interests.design && this.state.interests.code) 
     sortedArray.sort((a,b) => (b.scores.quality*b.scores.relevancy.code*b.scores.relevancy.design/2)-(a.scores.quality*a.scores.relevancy.code*a.scores.relevancy.design/2)) 
    else if(this.state.interests.design && this.state.interests.business) 
     sortedArray.sort((a,b) => (b.scores.quality*b.scores.relevancy.business*b.scores.relevancy.design/2)-(a.scores.quality*a.scores.relevancy.business*a.scores.relevancy.design/2)) 
    else if(this.state.interests.business && this.state.interests.code) 
     sortedArray.sort((a,b) => (b.scores.quality*b.scores.relevancy.business*b.scores.relevancy.code/2)-(a.scores.quality*a.scores.relevancy.business*a.scores.relevancy.code/2)) 
    else if(this.state.interests.business) 
     sortedArray.sort((a,b)=> (b.scores.quality*b.scores.relevancy.business-a.scores.quality*a.scores.relevancy.busines)) 
    else if(this.state.interests.code) 
     sortedArray.sort((a,b)=> (b.scores.quality*b.scores.relevancy.code-a.scores.quality*a.scores.relevancy.code)) 
    else if(this.state.interests.design) 
     sortedArray.sort((a,b)=> (b.scores.quality*b.scores.relevancy.design-a.scores.quality*a.scores.relevancy.design)) 
    console.log(JSON.stringify(sortedArray)) 
    return sortedArray 
} 

的想法是,如果他們選擇的設計,商業和代碼,然後將生成的數組應以質量進行排序,否則我的質量和所選擇的變量的混合。下面是數據:

[ 
    { 
     "name":"dasd", 
     "scores": { 
      "quality": 8, 
      "relevancy": { 
       "design": 3, 
       "code": 9, 
       "business": 5 
      } 
     }, 
     {...} 
    } 
    {...} 
] 

鑑於this.state是:

this.state = { 
     minutes: int, 
     interests: { 
       business: bool, 
       code: bool, 
       design: bool 
     } 
    } 
+3

請加數據,以及在文字符號。 –

+2

你是什麼意思的「*第一個元素總是在返回數組*」? 'sort'不會放置或移除任何東西。 – Bergi

+0

'sortedArray'具有與在操作開始時相同的第一個元素 – ocram

回答

1

使用公式不起作用排序對多個屬性的陣列。

鑑於與像{name:'Alice', age:8, grades:{math:12, gym:14}}結構「學生」對象的數組,下面不工作:

// This does not sort by math then gym, it sorts by the result of their products, which is meaningless 
students.sort((a,b) => (a.grades.math*a.grades.gym - b.grades.math*b.grades.gym)) 

正如你可以this Q/A看到,很難在JS實現,但也有庫,幫助使這個很直接,就像下劃線或lodash

有了你可以做到這一點lodash:

// This sorts by math score, then gym score 
_.sortBy(students, [s => s.grades.math, s => s.grades.gym]) 

現在適用於您的問題,沒有給人一種完整的解決方案,在這裏是一個暗示:

var data = [...]; 
var propertiesToSortBy = [ o => o.scores.quality ]; 
// Your rules below 
if (...) { 
    propertiesToSortBy = [ o => o.scores.quality, o => o.scores.relevancy.business ]; 
} 
else if (...) { 
    propertiesToSortBy = [ o => o.scores.relevancy.design, o => o.scores.relevancy.business ]; 
} 
else ... 
// Finally, sort 
_.sortBy(data, propertiesToSortBy);