最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Javascript: Push Filtered Array to Existing Array - Stack Overflow

programmeradmin3浏览0评论

I have a small todo app here: /

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

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

Something like this:

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

But it does not work.

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

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

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

Here is the fiddle: /

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

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

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

Something like this:

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

But it does not work.

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

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

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

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

Share Improve this question asked Jul 30, 2015 at 15:07 LoveAndHappinessLoveAndHappiness 10.2k22 gold badges74 silver badges107 bronze badges 4
  • 1 how about - this.tasks.archived = this.tasks.archived.concat(this.tasks.current.filter(function(task) { return task.pleted; });) – nril Commented Jul 30, 2015 at 15:20
  • it is working for me , Please make a fiddle with the buggy code – Antoine Eskaros Commented Jul 30, 2015 at 15:22
  • @TonyRaoulIscaros His fiddle is "working". Complete one task, archive it then do it with another. The first pleted task will disappear from the archived ones. – thllbrg 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 – Antoine Eskaros Commented Jul 30, 2015 at 15:28
Add a ment  | 

4 Answers 4

Reset to default 4

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

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

Working fiddle

You can change your function like this:

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

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

may be like this:

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

Here is the part I changed in your function:

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

And it seems to work. Here is the updated JSFiddle: http://jsfiddle/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.

发布评论

评论列表(0)

  1. 暂无评论