【问题标题】:JavaScript Map ArrayJavaScript 地图数组
【发布时间】: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


【解决方案1】:

您不需要编写自己的 map 函数,Array.prototype.map 已经完成了您的 map 函数所做的事情(还有更多,但这无关紧要)。

问题是:

  • 您的map 执行return newNotes; for 循环内,因此当循环仅完成其中一个元素时它会返回。
  • map 未正确调用您的回调。
  • 您的回调未按照 map 函数的预期执行。

对您的回调的调用应该是:

const result = callback(notes[i], i);

return newNotes; 应该在循环之后

然后你的回调应该创建一个新对象,其中包含传入的原始对象的属性,加上status: "completed" - 可能使用具有扩展语法的对象字面量,如下所示:

const output = map(notes, function(note, index) {
    return {...note, status: "completed" };
});

【讨论】:

  • 我认为他/她在练习回调函数?
  • @TropicsCold - 他们他们正在这样做,这就是为什么我没有告诉他们只使用Array.prototype.map。 :-)
【解决方案2】:

在 map 函数中,returned newNotes 数组位于 for 循环中,而不是在它之后。不过我建议使用内置的map 函数。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-11-11
    • 2020-06-29
    • 2017-12-02
    • 1970-01-01
    • 2023-02-21
    • 2020-01-22
    • 2019-03-31
    • 1970-01-01
    相关资源
    最近更新 更多