[英]How to get properties of one array of objects into another array of objects
Here's what I've got. 这就是我所拥有的。 One array of objects: 一个对象数组:
var teachers = [{
Year: 2016,
FullName: "Matt",
Age: 39
},
{
Year: 2016,
FullName: "Sara",
Age: 25
},
...
];
And another array of objects. 和另一个对象数组。 These would be nested like so: 这些将嵌套如下:
var students = [[
{
Year: 2016,
FullName: "Zoe"
Age: 8
}
],
[
{
Year: 2016,
FullName: "Lulu"
Age: 9
},
{
Year: 2016,
FullName: "Leo",
Age: 13
}
],
[ // empty array here
],
[
{
Year: 2016,
FullName: "Lotta",
Age: 11
}
]
...
];
How they are organized is that students[0] is a student of teachers[0]. 他们的组织方式是学生[0]是教师的学生[0]。 students[4] are students of teachers[4], and so forth. 学生[4]是教师的学生[4],等等。
What I was attempting to do what to take the FullName property, 'Students' in each student and put those values into an array of a new property of teachers called 'SundayStudents'. 我试图做什么采取FullName属性,每个学生的'学生',并将这些值放入一个名为'SundayStudents'的教师新属性的数组中。 So what I'd end up with would be: 所以我最终会得到的结果是:
teachers = [{
Year: 2016,
FullName: "Matt",
Age: 39,
SundayStudents: ["Zoe"]
},
{
Year: 2016,
FullName: "Sara",
Age: 25,
SundayStudents: ["Lulu", "Leo"]
},
...
];
I tried a nested for-loop, but the students array has varying numbers of objects in each sub-array, and it doesn't create an array for the new property. 我尝试了嵌套的for循环,但学生数组在每个子数组中有不同数量的对象,并且它不会为新属性创建数组。 I think I'm stuck. 我想我被卡住了。
for (var j = 0, leng = teachers.length; j < leng; j++) {
for (var k = 0, lent = students.length; k < lent; k++)
Teachers[i].SundayStudents = Students[j][k].FullName;
}
Any hints are welcome. 任何提示都是受欢迎的。
You could just iterate and check if the target element exist. 您可以迭代并检查目标元素是否存在。 Then you could make a new property with the mapped names. 然后,您可以使用映射的名称创建一个新属性。
var teachers = [{ Year: 2016, FullName: "Matt", Age: 39 }, { Year: 2016, FullName: "Sara", Age: 25 }], students = [[{ Year: 2016, FullName: "Zoe", Age: 8 }], [{ Year: 2016, FullName: "Lulu", Age: 9 }, { Year: 2016, FullName: "Leo", Age: 13 }], [{ Year: 2016, FullName: "Lotta", Age: 11 }]]; students.forEach(function (a, i) { if (Array.isArray(a) && teachers[i]) { teachers[i].SundayStudents = a.map(function (b) { return b.FullName; }); } }); console.log(teachers);
Don't overdo the loops. 不要过度循环。 You can use the outer loop's index for both teachers and student arrays. 您可以为教师和学生数组使用外循环索引。
for (var i = 0; i < teachers.length; i++) {
teachers[i].SundayStudents = []
for (var j = 0; j < students[i].length; j++) {
teachers[i].SundayStudents.push(students[i][j].FullName);
}
}
well, if all students' elements are arrays (a 2-dimensional array) then you just need to add one more for loop to scrape the inner array 好吧,如果所有学生的元素都是数组(一个二维数组),那么你只需要再添加一个for循环来擦除内部数组
teachers[i].SundayStudents = [];
for (var j = 0; j < teachers.length; j++) {
for (var k = 0; k < students.length; k++) {
var studentSub = students[k];
for (var l = 0; l < studentSub.length; l++) {
teachers[j].SundayStudents.push(students[k][l].FullName);
}
}
}
You are using students.length in the inner for loop which should have been students[j].length because it is an array of array. 你在内部for循环中使用students.length,这应该是student [j] .length,因为它是一个数组数组。
var teachers = [{
Year: 2016,
FullName: "Matt",
Age: 39
},
{
Year: 2016,
FullName: "Sara",
Age: 25
}
]
var students = [[
{
Year: 2016,
FullName: "Zoe",
Age: 8
}
],
[
{
Year: 2016,
FullName: "Lulu",
Age: 9
},
{
Year: 2016,
FullName: "Leo",
Age: 13
}
],
[ // empty array here
],
[
{
Year: 2016,
FullName: "Lotta",
Age: 11
}
]
]
for(var i = 0; i < teachers.length; i++){
teachers[i].SundayStudents = []
for(var j = 0; j < students[i].length; j++){
teachers[i].SundayStudents[j] = students[i][j].FullName
}
}
console.log(teachers)
Loop through teachers
. 循环通过teachers
。 Copy the object. 复制对象。 Add SundayStudents
as array. 将SundayStudents
添加为数组。 Loop through students
of the corresponding index and push the FullName
to SundayStudents
: 循环遍历相应索引的students
并将 FullName
推送到SundayStudents
:
var teachers = [{ Year: 2016, FullName: "Matt", Age: 39 }, { Year: 2016, FullName: "Sara", Age: 25 }]; var students = [ [{ Year: 2016, FullName: "Zoe", Age: 8 }], [{ Year: 2016, FullName: "Lulu", Age: 9 }, { Year: 2016, FullName: "Leo", Age: 13 }] ]; var r = []; teachers.forEach(function(obj, i) { var o = {}; o = obj; o.SundayStudents = []; students[i].forEach(function(d) { o.SundayStudents.push(d.FullName); }); r.push(o); }); console.log(r);
The solution using Array.forEach
and Array.map
functions: 使用Array.forEach
和Array.map
函数的解决方案:
teachers.forEach(function(v, i, arr) { // arr - the array that forEach() is being applied to
if (Array.isArray(students[i]) && students[i].length) { // check for non-empty array
arr[i].SundayStudents = students[i].map((st) => st.FullName);
}
});
console.log(JSON.stringify(teachers, 0, 4));
The output: 输出:
[
{
"Year": 2016,
"FullName": "Matt",
"Age": 39,
"SundayStudents": [
"Zoe"
]
},
{
"Year": 2016,
"FullName": "Sara",
"Age": 25,
"SundayStudents": [
"Lulu",
"Leo"
]
}
...
]
This appears to be a simple job of maps. 这似乎是一个简单的地图工作。
var students = [[ { Year: 2016, FullName: "Zoe", Age: 8 } ], [ { Year: 2016, FullName: "Lulu", Age: 9 }, { Year: 2016, FullName: "Leo", Age: 13 } ], [ // empty array here ], [ { Year: 2016, FullName: "Lotta", Age: 11 } ]], teachers = [{ Year: 2016, FullName: "Matt", Age: 39 }, { Year: 2016, FullName: "Sara", Age: 25 }, { Year: 2016, FullName: "Yellow Beard", Age: 39 }, { Year: 2016, FullName: "Professor Oclitus", Age: 25 }, ]; teachers = teachers.map((t,i) => (t.SundayStudents = students[i].map(s => s.FullName),t)); console.log(teachers);
Wow.. i just noticed there was an answer exactly the same. 哇..我刚才注意到答案完全相同。 So let me give an ES5 compatible version. 那么让我给出ES5兼容版本。
teachers = teachers.map(function(t,i) {
t.SundayStudents = students[i].map(function(s) {
return s.FullName;
});
return t;
});
仅仅因为它是可能的,这是一个单行答案
teachers.map((t, i) => (t.sundayStudents = students[i].map(s => s.FullName), t));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.