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

时间:2016-06-20 13:46:16

标签: javascript arrays loops

这是我所拥有的。一个对象数组:

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
                  }
                ]
                ...
               ];

他们的组织方式是学生[0]是教师的学生[0]。学生[4]是教师的学生[4],等等。

我试图采取什么样的FullName属性,'学生'在每个学生中,将这些价值观放入一个名为“星期日学生”的教师的新属性中。所以我最终会得到:

teachers = [{
             Year: 2016,
             FullName: "Matt",
             Age: 39,
             SundayStudents: ["Zoe"]
            },
            {
              Year: 2016,
              FullName: "Sara",
              Age: 25,
              SundayStudents: ["Lulu", "Leo"]
            },
             ...
          ];

我尝试了嵌套的for循环,但学生数组在每个子数组中有不同数量的对象,并且它不会为新属性创建数组。我想我已经卡住了。

  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;
  }

欢迎任何提示。

8 个答案:

答案 0 :(得分:1)

您可以迭代并检查目标元素是否存在。然后你可以创建一个带有映射名称的新属性。

&#13;
&#13;
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);
&#13;
&#13;
&#13;

答案 1 :(得分:1)

不要过度循环。您可以为教师和学生数组使用外循环索引。

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);
  }
}

答案 2 :(得分:1)

好吧,如果所有学生都是&#39;元素是数组(一个二维数组)然后你只需要再添加一个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);
        }
    }
}

答案 3 :(得分:1)

你在内部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)

答案 4 :(得分:1)

循环teachers复制对象。将SundayStudents添加为数组。循环显示相应索引的students推送 FullNameSundayStudents

&#13;
&#13;
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);
&#13;
&#13;
&#13;

答案 5 :(得分:1)

使用Array.mapteachers.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)); 函数的解决方案:

[
    {
        "Year": 2016,
        "FullName": "Matt",
        "Age": 39,
        "SundayStudents": [
            "Zoe"
        ]
    },
    {
        "Year": 2016,
        "FullName": "Sara",
        "Age": 25,
        "SundayStudents": [
            "Lulu",
            "Leo"
        ]
    }
    ...
]

输出:

sudo modprobe iscsi_tcp

答案 6 :(得分:1)

这似乎是一个简单的地图工作。

&#13;
&#13;
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);
&#13;
&#13;
&#13;

哇..我刚才注意到答案完全相同。所以,让我给出ES5兼容版本。

teachers = teachers.map(function(t,i) {
                                        t.SundayStudents = students[i].map(function(s) {
                                                                                         return s.FullName;
                                                                                       });
                                        return t;
                                      });

答案 7 :(得分:0)

仅仅因为这是可能的,这是一个单行答案

teachers.map((t, i) => (t.sundayStudents = students[i].map(s => s.FullName), t));