1

I have a small todo app here: http://jsfiddle.net/ccLm46sn/

Two arrays, one contains completed tasks, the other archived tasks.

Whenever I execute the function archiveCompleted I want all completed tasks to be pushed on to the array with the archived tasks and the completed array then deleted.

Something like this:

this.tasks.archived.push({ this.tasks.current.filter(function(task) { return task.completed; }); }); 

But it does not work.

This here works fine, by overwriting the archived array with the completed array:

this.tasks.archived = this.tasks.current.filter(function(task) { return task.completed; }); 

But I don't want to overwrite the archived array, just to push into that:

Here is the fiddle: http://jsfiddle.net/ccLm46sn/

4
  • 1
    how about - this.tasks.archived = this.tasks.archived.concat(this.tasks.current.filter(function(task) { return task.completed; });) Commented Jul 30, 2015 at 15:20
  • it is working for me , Please make a fiddle with the buggy code Commented Jul 30, 2015 at 15:22
  • @TonyRaoulIscaros His fiddle is "working". Complete one task, archive it then do it with another. The first completed task will disappear from the archived ones. Commented Jul 30, 2015 at 15:25
  • No I mean the push method is working , okay now i figure out more than one element cause the problem Commented Jul 30, 2015 at 15:28

4 Answers 4

4

Use concat to merge the the archived tasks with the completed ones.

// assigns to the archived object the completed tasks object var completedTasks = this.tasks.current.filter(function(task) { return task.completed; }); this.tasks.archived = this.tasks.archived.concat(completedTasks); 

Working fiddle

Sign up to request clarification or add additional context in comments.

Comments

4

You can change your function like this:

this.tasks.archived.push.apply(this.tasks.archived, this.tasks.current.filter(function(task) { return task.completed; })); 

.apply is used to combine arrays here is a working fiddle

4 Comments

That's a nice solution, but the fiddle doesn't work. The above code is correct. There is an extra pair of braces '()' in the fiddle.
this is the best solution. I never new about "push.apply". Thanks @brso05
@khichar.anil no problem!
@khichar.anil apply is a function method and can be called on any function. It is not specific to push. Here is the documentation: developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
1

may be like this:

this.tasks.archived = this.tasks.archived.concat(this.tasks.current.filter(function(task) { return task.completed; })); 

Comments

1

Here is the part I changed in your function:

var completed_tasks = this.tasks.current.filter(function(task) { return task.completed; }); for (var i = 0; i < completed_tasks.length; i++) { this.tasks.archived.push(completed_tasks[i]); } 

And it seems to work. Here is the updated JSFiddle: http://jsfiddle.net/ccLm46sn/1/

I don't know if it is the best and most elegant solution, but it works as far as I have tested it.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.