修改对象数组的对象
我想修改一系列对象的对象(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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
做你追求的。我自由地自由改变了那里,但这应该是相当自我解释的。
我认为您的问题是您不会在任何地方存储结果...
第2行显示了
output
array的创建(在for loop启动之前),然后将每个学生的数据对象推开到第4行上的数组。最后,循环完成并关闭后,
输出
数组将记录到控制台。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.以前的答案是完美的,但是如果您想要一种更有功能的方法,则可以使用地图迭代阵列。如果您不想修改原始数组,则可以使用object.sign如示例中。
如果您不关心修改原始数组,则可以做类似的事情:
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.
If you do not care about modifying your original array, you can do something like this: