JavaScript 地图数组
JavaScript Map Array
名为 '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",
},
];
我想更新所有笔记的状态为'completed',错误是代码只更新了第一个对象
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);
我是训练回调函数的,这次训练代码遇到了写代码的问题如果有有用的资源可以分享给我
您不需要编写自己的 map
函数,Array.prototype.map
已经完成了您的 map
函数所做的事情(还有更多,但这无关紧要)。
问题是:
- 你的
map
在 for
循环中 return newNotes;
执行,所以当循环只完成一个时 returns的元素。
map
没有正确调用您的回调。
- 您的回调未按照
map
函数的预期执行。
对回调的调用应该只是:
const result = callback(notes[i], i);
并且 return newNotes;
应该是 在 循环之后。
然后你的回调应该创建一个新对象,其中包含传入的原始对象的属性,加上 status: "completed"
- 可能使用带有扩展语法的对象文字,如下所示:
const output = map(notes, function(note, index) {
return {...note, status: "completed" };
});
在 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);
名为 '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",
},
];
我想更新所有笔记的状态为'completed',错误是代码只更新了第一个对象
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);
我是训练回调函数的,这次训练代码遇到了写代码的问题如果有有用的资源可以分享给我
您不需要编写自己的 map
函数,Array.prototype.map
已经完成了您的 map
函数所做的事情(还有更多,但这无关紧要)。
问题是:
- 你的
map
在for
循环中return newNotes;
执行,所以当循环只完成一个时 returns的元素。 map
没有正确调用您的回调。- 您的回调未按照
map
函数的预期执行。
对回调的调用应该只是:
const result = callback(notes[i], i);
并且 return newNotes;
应该是 在 循环之后。
然后你的回调应该创建一个新对象,其中包含传入的原始对象的属性,加上 status: "completed"
- 可能使用带有扩展语法的对象文字,如下所示:
const output = map(notes, function(note, index) {
return {...note, status: "completed" };
});
在 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);