修改对象数组的对象

发布于 2025-02-11 06:13:05 字数 1461 浏览 2 评论 0原文

我想修改一系列对象的对象(jsonstudent)。

   [
    { Student: 'Riya', Gender: 'Female', Pet: 'Timmy' },
    { Student: 'Happy', Gender: 'Male', Pet: 'Harry' },
    { Student: 'Tiya', Gender: 'Female', Pet: 'Timmy' },
   ]

而且我的预期输出是(输入):

  [ 
    {
     name: 'Riya',
     gender: 'Female',
     pet: 'Timmy',
     isActive: true,
     createdAt: 2022-06-28T17:55:53.907Z,
     updatedAt: 2022-06-28T17:55:53.907Z
   },
   {
     name: 'Happy',
     gender: 'Male',
     pet: 'Harry',
     isActive: true,
     createdAt: 2022-06-28T17:55:53.913Z,
     updatedAt: 2022-06-28T17:55:53.913Z
  },
  {
     name: 'Tiya',
     gender: 'Female',
     pet: 'Timmy',
     isActive: true,
     createdAt: 2022-06-28T17:55:53.919Z,
     updatedAt: 2022-06-28T17:55:53.919Z
    }
   ]

为此,我使用了此代码行,但是我只能在输出中获得第一个对象,任何人都可以帮助我将所有对象作为我的预期输出。

        console.log(jsonStudent);
        for (var i = 0; i < jsonStudent.length; i++) {
            const nameVal = jsonStudent[i].Student;
            const genderVal = jsonStudent[i].Gender;
            const petVal = jsonStudent[i].Pet;
            const now = new Date();
            const input = {
                name: nameVal,
                gender: genderVal,
                pet: petVal,
            };
            input.isActive = true;
            input.createdAt = now;
            input.updatedAt = now;
            console.log(input);

I want to modify the objects of an array of objects (jsonStudent).

   [
    { Student: 'Riya', Gender: 'Female', Pet: 'Timmy' },
    { Student: 'Happy', Gender: 'Male', Pet: 'Harry' },
    { Student: 'Tiya', Gender: 'Female', Pet: 'Timmy' },
   ]

And my expected output is(input):

  [ 
    {
     name: 'Riya',
     gender: 'Female',
     pet: 'Timmy',
     isActive: true,
     createdAt: 2022-06-28T17:55:53.907Z,
     updatedAt: 2022-06-28T17:55:53.907Z
   },
   {
     name: 'Happy',
     gender: 'Male',
     pet: 'Harry',
     isActive: true,
     createdAt: 2022-06-28T17:55:53.913Z,
     updatedAt: 2022-06-28T17:55:53.913Z
  },
  {
     name: 'Tiya',
     gender: 'Female',
     pet: 'Timmy',
     isActive: true,
     createdAt: 2022-06-28T17:55:53.919Z,
     updatedAt: 2022-06-28T17:55:53.919Z
    }
   ]

For that I used this lines of code, but instead of all objects I am getting only the 1st object in output,Can anyone please help me to get all the objects as my expected output.

        console.log(jsonStudent);
        for (var i = 0; i < jsonStudent.length; i++) {
            const nameVal = jsonStudent[i].Student;
            const genderVal = jsonStudent[i].Gender;
            const petVal = jsonStudent[i].Pet;
            const now = new Date();
            const input = {
                name: nameVal,
                gender: genderVal,
                pet: petVal,
            };
            input.isActive = true;
            input.createdAt = now;
            input.updatedAt = now;
            console.log(input);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

醉殇 2025-02-18 06:13:05
console.log(jsonStudent);
const output = [];
for (var i = 0; i < jsonStudent.length; i++) {
  output.push({
    name: jsonStudent[i].Student,
    gender: jsonStudent[i].Gender,
    pet: jsonStudent[i].Pet,
    isActive: true,
    createdAt: new Date(),
    updatedAt: new Date()
  });
}
console.log(output);

做你追求的。我自由地自由改变了那里,但这应该是相当自我解释的。

我认为您的问题是您不会在任何地方存储结果...

第2行显示了output array的创建(在for loop启动之前),然后将每个学生的数据对象推开到第4行上的数组。

最后,循环完成并关闭后,输出数组将记录到控制台。

console.log(jsonStudent);
const output = [];
for (var i = 0; i < jsonStudent.length; i++) {
  output.push({
    name: jsonStudent[i].Student,
    gender: jsonStudent[i].Gender,
    pet: jsonStudent[i].Pet,
    isActive: true,
    createdAt: new Date(),
    updatedAt: new Date()
  });
}
console.log(output);

Does what you're after. I've taken the liberty of changing a fair amount there, but it should be fairly self explanatory.

I think your issue was that you weren't storing the results anywhere...

Line 2 shows the creation of the output array (before the for loop starts), and then each student's data object is pushed on to the array on line 4.

Finally, once the loop is finished and closed, the output array is logged to the console.

篱下浅笙歌 2025-02-18 06:13:05

以前的答案是完美的,但是如果您想要一种更有功能的方法,则可以使用地图迭代阵列。如果您不想修改原始数组,则可以使用object.sign如示例中。

let newStudents = jsonStudents.map(student => {
    let newStudent = Object.assign({}, student);
    newStudent.isActive = true;
    newStudent.createdAt = new Date();
    newStudent.updatedAt = new Date();
    return newStudent;
});
console.log(newStudents);

如果您不关心修改原始数组,则可以做类似的事情:

    let newStudents = jsonStudents.map(student => {
        student.isActive = true;
        student.createdAt = new Date();
        student.updatedAt = new Date();
        return student;
    });

The previous answer is perfect, but if you want a more functional approach, you can use map to iterate your array. If you do not want to modify your original array, you can use object.assign as in the example.

let newStudents = jsonStudents.map(student => {
    let newStudent = Object.assign({}, student);
    newStudent.isActive = true;
    newStudent.createdAt = new Date();
    newStudent.updatedAt = new Date();
    return newStudent;
});
console.log(newStudents);

If you do not care about modifying your original array, you can do something like this:

    let newStudents = jsonStudents.map(student => {
        student.isActive = true;
        student.createdAt = new Date();
        student.updatedAt = new Date();
        return student;
    });
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文