简体   繁体   English

如何将一个对象数组的属性转换为另一个对象数组

[英]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.forEachArray.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.

相关问题 如何从一个对象数组中获取值到另一个对象数组中 - How to get values from one array of objects into another array of objects 将属性从一个对象数组添加到另一个对象 - Add properties from one array of objects to another 如何在javascript中更改对象属性之一传播到另一个对象的数组 - How to change array that one of the objects properties spread in to another in javascript 如何从JavaScript中另一个对象数组中的对象数组获取底层属性 - How to get bottom-level properties from array of objects inside another array of objects in JavaScript 如何获取对象数组中对象的所有属性值的数组? - How to get an array of the values of all properties of objects inside an array of objects? 用另一个对象数组过滤一个对象数组 - Filtering one array of objects with another array of objects 如何在JS中获取对象数组的特定属性? - How to get specific properties of array of objects in JS? 如何根据另一个对象数组的值获取一个对象数组键的值? - how to get the values of one array of objects key based on values of another array of objects? 如何在 TypeScript 中的对象数组中将数据从一个对象数组获取到另一个对象数组中 - How to Get Data From One Array of Objects into another in Array of Objects in TypeScript 按对象从对象数组中获取属性 - Get properties from array of objects group by objects
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM