gpt4 book ai didi

javascript - 在 json 中组合项目

转载 作者:行者123 更新时间:2023-11-29 19:05:32 25 4
gpt4 key购买 nike

我能够编写一个解决方案,但我想知道是否有更优雅、更高效的方法。

    var arr = [];

//I have a array like this:
arr = [{
teacherid: 10,
teacherName: 'andy',
age: 30,
studentid: 1,
studentName: 'A',
studentAge: 10
},
{
teacherid: 10,
teacherName: 'andy',
age: 30,
studentid: 2,
studentName: 'B',
studentAge: 11
}, {
teacherid: 10,
teacherName: 'andy',
age: 30,
studentid: 3,
studentName: 'C',
studentAge: 12
},

{
teacherid: 11,
teacherName: 'May',
age: 40,
studentid: 4,
studentName: 'D',
studentAge: 9
}, {
teacherid: 11,
teacherName: 'May',
age: 40,
studentid: 5,
studentName: 'E',
studentAge: 12
}
];


//want to group the students info in each indiviual teacher like this:
//*
[{
"teacherid": 10,
"teacherName": "andy",
"age": 30,
"studentArr": [{
"studentid": 1,
"studentName": "A",
"studentAge": 10
}, {
"studentid": 2,
"studentName": "B",
"studentAge": 11
}, {
"studentid": 3,
"studentName": "C",
"studentAge": 12
}]
}, {
"teacherid": 11,
"teacherName": "May",
"age": 40,
"studentArr": [{
"studentid": 4,
"studentName": "D",
"studentAge": 9
}, {
"studentid": 5,
"studentName": "E",
"studentAge": 12
}]
}
}]
*//



//my solution
var result = [];
var first = true;
arr = _.sortBy(arr, 'teacherid');


(function() {
for (var i = 0; i < arr.length; i++) {
for (var j = i; j < arr.length; j++) {
console.log("comparing i:" + i + " with j:" + j);

if (i === j && j === arr.length) {
console.log("exit " + i + " : " + j)
return;
} else if (arr[i].teacherid === arr[j].teacherid) {
if (first) {
result.push({
teacherid: arr[j].teacherid,
teacherName: arr[j].teacherName,
age: arr[j].age,
studentArr: [{
studentid: arr[j].studentid,
studentName: arr[j].studentName,
studentAge: arr[j].studentAge
}]
})
} else {
//find in result arr, the obj that contain this teacher and push the student in the array
var teacher = _.find(result, function(item) {
return (item.teacherid === arr[j].teacherid)
})
teacher.studentArr.push({
studentid: arr[j].studentid,
studentName: arr[j].studentName,
studentAge: arr[j].studentAge
})
}
first = false;

} else {
first = true;
i = j - 1;
j = arr.length; //end inner loop, go back to outer loop and check next teacherID
}
}
}

})()


console.log(JSON.stringify(result));

如果让其他程序员阅读它,这将是一件令人头疼的事情。我想知道是否有比这更好的解决方案(更有效和/或更具可读性)。我觉得我是用暴力破解的

最佳答案

您可以为教师使用带有哈希表的闭包。

var array = [{ teacherid: 10, teacherName: 'andy', age: 30, studentid: 1, studentName: 'A', studentAge: 10 }, { teacherid: 10, teacherName: 'andy', age: 30, studentid: 2, studentName: 'B', studentAge: 11 }, { teacherid: 10, teacherName: 'andy', age: 30, studentid: 3, studentName: 'C', studentAge: 12 }, { teacherid: 11, teacherName: 'May', age: 40, studentid: 4, studentName: 'D', studentAge: 9 }, { teacherid: 11, teacherName: 'May', age: 40, studentid: 5, studentName: 'E', studentAge: 12 }],
grouped = array.reduce(function (hash) {
return function (r, a) {
if (!hash[a.teacherid]) {
hash[a.teacherid] = {
teacherid: a.teacherid,
teacherName: a.teacherName,
studentArr: []
};
r.push(hash[a.teacherid]);
}
hash[a.teacherid].studentArr.push({
studentid: a.studentid,
studentName: a.studentName,
studentAge: a.studentAge
});
return r;
};
}(Object.create(null)), []);

console.log(grouped);
.as-console-wrapper { max-height: 100% !important; top: 0; }

关于javascript - 在 json 中组合项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43161777/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com