【发布时间】:2022-01-11 07:57:51
【问题描述】:
名为“notes”的数组包含 5 个对象,每个对象都有键
var notes = [
{
title: "Home",
message: "is a good story",
status: 'new',
author:"dala",
},
{
title: "School",
message: "Have to go everday",
status: 'new',
author:"aisha",
},
{
title: "study",
message: "we have exam to pass",
status: 'new',
author:"Omar",
},
{
title: "Work",
message: "dead line is close",
status: 'new',
author:"Said",
},
{
title: "homework",
message: "as today we need to do it",
status: 'new',
author:"Amal",
},
];
我想将所有笔记的状态更新为“完成”,错误是代码只更新了第一个对象
function map(notes,callback){
const newNotes =[];
for(var i=0; i<notes.length; i++) {
const result = callback(notes[i].status = "completed",i);
newNotes.push(result);
return newNotes;
}
}
var outp = map(notes,function(value, i){
console.log(i)
for(var a= 0; a<value.length; a++){
return notes;
}
})
console.log(outp);
我在训练回调函数,这个训练代码是写代码时遇到的问题如果你有有用的资源可以学习,请与我分享
【问题讨论】:
-
在回调参数中使用
notes[i].status = "completed"是错误的。
标签: javascript arrays callback maps