2017-06-05 171 views
-1

我有這樣一組數據,我沒有控制(其託管API的地方,我都進不去)拉屬性對象作爲父對象

let x = [ 
    {name: "james", age: 10, school: {id:1, name:"London"}}, 
    {name: "james_2", age: 11, school: {id:1, name:"London"}}, 
    {name: "john", age: 12, school: {id:2, name:"India"}}, 
    {name: "johnny", age: 56, school: {id:3, name:"USA"}} 
] 

我怎能組全校第一?我需要的格式是這樣

[{ 
    id:1, 
    name: "London", 
    data: [{ 
     name: "james", age: 10, school: {id:1, name:"London" 
    },{name: "james_2", age: 11, school: {id:1, name:"London"}}] 
}] 

回答

2

您可以使用_.groupBy()並將結果返回映射到數組:

const data = [{"name":"james","age":10,"school":{"id":1,"name":"London"}},{"name":"james_2","age":11,"school":{"id":1,"name":"London"}},{"name":"john","age":12,"school":{"id":2,"name":"India"}},{"name":"johnny","age":56,"school":{"id":3,"name":"USA"}}]; 
 

 
const result = _(data) 
 
    .groupBy('school.name') 
 
    .map((group) => ({ // or Object.assign({}, group[0].school, { data: group }); 
 
    ...group[0].school, 
 
    data: group 
 
    })) 
 
    .value(); 
 

 
console.log(result);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.min.js"></script>

您可以通過組合使用Array#reduceMap離不開lodash同樣的事情:

const data = [{"name":"james","age":10,"school":{"id":1,"name":"London"}},{"name":"james_2","age":11,"school":{"id":1,"name":"London"}},{"name":"john","age":12,"school":{"id":2,"name":"India"}},{"name":"johnny","age":56,"school":{"id":3,"name":"USA"}}]; 
 

 
const result = [...data.reduce((map, item) => { 
 
    const school = map.get(item.school.name) || { // or Object.assign({}, { item.school }, { data: [] }) 
 
    ...item.school, data: [] 
 
    }; 
 
    
 
    school.data.push(item); 
 
    
 
    map.set(item.school.name, school); 
 
    
 
    return map; 
 
}, new Map()).values()] 
 

 
console.log(result);

+0

什麼是這個? _(數據) –

+0

組[0] .school爲什麼在這裏使用[0]? –

+0

_(數據)啓動一個lodash鏈。 group [0] .school用於從組的第一個元素中獲取學校名稱和ID。 –

0

您可以創建一個在其ID存儲每個學校一個哈希對象,所以你可以把學生吧:

var hash=x.reduce(function(obj,el){ 
(obj[el.school.id]=obj[el.school.id]||{id:el.school.id,name:el.school.name,data:[]}).data.push({name:el.name,age:el.age}); 
    return obj; 
},{}); 

var arr=Object.values(hash); 

http://jsbin.com/vedajijiye/edit?console

0

您可以使用reduce()方法:

let data = [ 
 
    {name: "james", age: 10, school: {id:1, name:"London"}}, 
 
    {name: "james_2", age: 11, school: {id:1, name:"London"}}, 
 
    {name: "john", age: 12, school: {id:2, name:"India"}}, 
 
    {name: "johnny", age: 56, school: {id:3, name:"USA"}} 
 
]; 
 
let schools = data.reduce((acc, x) => { 
 
    // Check if the school already exists 
 
    let school = acc.find(y => y.id === x.school.id); 
 
    if (!school) { 
 
    // If not, the current school is the school of the current person 
 
    school = x.school; 
 
    school.data = []; 
 
    acc.push(school); 
 
    } else { 
 
    // Prevent duplication of school objects with the same id 
 
    x.school = school; 
 
    } 
 
    school.data.push(x); 
 
    return acc; 
 
}, []); 
 
console.log(schools);

0

可以使用lodash庫GROUPBY

let x = [ 
    {name: "james", age: 10, school: {id:1, name:"London"}}, 
    {name: "james_2", age: 11, school: {id:1, name:"London"}}, 
    {name: "john", age: 12, school: {id:2, name:"India"}}, 
    {name: "johnny", age: 56, school: {id:3, name:"USA"}} 
] 


var result2 = _.chain(x) 
    .groupBy(function(o){return o.school.name}) 
    .pairs() 
    .map(function (currentItem) { 
     return _.object(_.zip(["school", "name"], currentItem)); 
    }) 
    .value(); 
console.log(result2); 

檢查此的jsfiddle: - http://jsfiddle.net/6yasy30a/

+0

pairs?那個有什麼用? –

+0

@AlanJenshen _pairs(): - 創建對象的鍵值對的二維數組,例如: [[key1,value1],[key2,value2]]。 _.pairs({'barney':36,'fred':40}); // => [['barney',36],['fred',40]](迭代順序不能保證) –

+0

你的邏輯比Ori的代碼更復雜 –

0

let x = [ 
 
    {name: "james", age: 10, school: {id:1, name:"London"}}, 
 
    {name: "james_2", age: 11, school: {id:1, name:"London"}}, 
 
    {name: "john", age: 12, school: {id:2, name:"India"}}, 
 
    {name: "johnny", age: 56, school: {id:3, name:"USA"}} 
 
]; 
 

 
let result = []; 
 
for(var i = 0; i < x.length; i++) { 
 
    var person = x[i]; 
 
    var school = result.find(s => s.id === person.school.id); 
 
    if(!school) { 
 
    school = { 
 
     id: person.school.id, 
 
     name: person.school.name, 
 
     data: [] 
 
    }; 
 
    result.push(school); 
 
    } 
 
    school.data.push(person); 
 
} 
 

 
console.log(result);

0

使用減少,簡單地推向一個新的對象進入你的結果數組,如果id不存在,否則只需更新t他在找到的對象中的數據數組

const myArray = x.reduce((res, data) => { 
    const found = res.find(o => o.id === data.school.id); 

    if (!found) { 
    res.push({ 
     id: data.school.id, 
     name: data.school.name, 
     data: [data] 
    }); 
    } else { 
    found.data.push(data); 
    } 
    return res; 
}, []); 


console.log(myArray)