简体   繁体   English

为什么我的 function 在尝试将值推送到我的数组时不起作用?

[英]Why does my function not work when trying to push values onto my array?

 const todo = [{ task: 'Wash Plates', day: 'Monday' },{ task: 'Clean Pc', day: 'Tuesday' },] let addTask = todo.push((todoTask, date) => { todo.task = todoTask, todo.day = date }) addTask('car', 'monday') for (var i = 0; i < todo.length; i++) { console.log(`${i+1}. ${todo[i].task} on ${todo[i].day}`); }

Why is the addTask not a function, even when i use normal functions (not arrows) it has the same result.为什么 addTask 不是 function,即使我使用普通功能(不是箭头)它也有相同的结果。 The loop below is fine and so is the array above.下面的循环很好,上面的数组也很好。 I want to be able push values onto my todo list.我希望能够将值推送到我的待办事项列表中。 Can someone help please.有人可以帮忙吗。

You've got .push and the arrow function mixed up.你有.push和箭头 function 混淆了。 Try this:尝试这个:

let addTask = (todoTask, date) => {
  todo.push({
    task: todoTask,
    day: date
  })
};

addTask is not a function. addTask不是 function。 You're calling todo.push() with a function as the argument, then you're assigning the return value to addTask .您使用 function 作为参数调用todo.push() ,然后将返回值分配给addTask push() returns the new length of the array, so you're adding the function to the array and then setting addTask to the array length. push()返回数组的新长度,因此您将 function 添加到数组中,然后将addTask设置为数组长度。

You need to take the function out of the push() argument.您需要将 function 从push()参数中取出。 Then you need to use correct syntax for an object literal.然后,您需要对 object 文字使用正确的语法。

 const todo = [{ task: 'Wash Plates', day: 'Monday' }, { task: 'Clean Pc', day: 'Tuesday' }, ] let addTask = (todoTask, date) => todo.push({ task: todoTask, todo: date }) addTask('car', 'monday') for (var i = 0; i < todo.length; i++) { console.log(`${i+1}. ${todo[i].task} on ${todo[i].day}`); }

You are pushing the following function into the end of the todo array:您将以下 function 推入todo数组的末尾:

(todoTask, date) => { ... }  

after which the call to Array.push returns the new length of the todo array , hence you get the length (a number) as a returned value to addTask .之后对Array.push 的调用返回todo array 的新长度,因此您将长度(一个数字)作为返回值传递给addTask

The definition of addTask function should be: addTask function 的定义应该是:

let addTask = (task, day) => todo.push({task ,day});

Similar to the others, but you can change your variable names to allow for the ES6 object construction to be minimized.与其他类似,但您可以更改变量名称以允许最小化 ES6 object 构造。

 const todo = [{ task: 'Wash Plates', day: 'Monday' }, { task: 'Clean Pc', day: 'Tuesday' }, ] let addTask = (task, day) => todo.push({task, day}); addTask('car', 'monday') for (var i = 0; i < todo.length; i++) { console.log(`${i+1}. ${todo[i].task} on ${todo[i].day}`); }

The correct notation for an arrow function is this: (args) => {}.箭头 function 的正确表示法是:(args) => {}。 addTask is not actually in function format, it just receives the push return, to resolve this do: addTask 实际上不是 function 格式,它只是接收推送返回,解决这个问题:

let addTask = (todoTask, date) => todo.push({task: todoTask, day: date})

eg例如

 const todo = [{ task: 'Wash Plates', day: 'Monday' },{ task: 'Clean Pc', day: 'Tuesday' },] let addTask = (todoTask, date) => todo.push({task: todoTask, day: date}) addTask('car', 'monday') for (var i = 0; i < todo.length; i++) { console.log(`${i+1}. ${todo[i].task} on ${todo[i].day}`); }

you don't need a separate function do add some values to your array.您不需要单独的 function 向您的数组添加一些值。 Try this:尝试这个:

array.push({
   task: todoTask,
   day: date
})

although if you want to make a function:虽然如果你想制作一个 function:

const addTask = (task, day) => todo.push({task, day})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM