我有这组数据,我无法控制(它在某个地方托管了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"}}]
}]
答案 0 :(得分: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#reduce
与Map
结合使用,您可以在不使用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);
答案 1 :(得分: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);
答案 2 :(得分: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);
&#13;
答案 3 :(得分: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/
答案 4 :(得分: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);
答案 5 :(得分:0)
如果id不存在,使用reduce并简单地将新对象推送到结果数组中,否则只需更新找到的对象中的数据数组
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)