0

I wish to simulate a taskbar (of running tasks/apps). I plan to store tasks something like this:

(function ()
{
    var tasks = [];

    addTask = function (taskName, taskWindow)
    {
        if (!tasks[taskName]) { tasks[taskName] = []; }
        tasks[taskName].push({ taskWindow: taskWindow, taskName: taskName});
    };
    removeTask = function (taskName, taskWindow)
    {
        if (tasks[taskName])
        {
            //Somehow remove the object from the array
        }        
    };
}());

How should I write removeTask() to remove the correct element from this jagged array?

2
  • just do a for loop over tasks[taskName], find the one by taskID, use array.splice, and close this question Commented Feb 16, 2013 at 5:41
  • You might want to consider backbone.js or some other framework for MVC. You'll find such a thing very helpful. Commented Feb 16, 2013 at 5:42

2 Answers 2

1

I suggest using object to store your tasks, because it will make your ( specific to your requirement, I am not talking about Array vs Object) code cleaner and easier to maintain

var taskManager = (function(){
    function taskManager(tasks){
      // Do your tasks validation before passing to this.
      var this.tasks = tasks || {}; // tasks value is not private here         
    }
    // Assuming taskID would be unique value
    taskManager.prototype.addTask = function (taskName, taskID){
       if ( !this.tasks[taskID] ) {
          this.tasks[taskID] = { taskID: taskID, taskName: taskName }; 
       }
    };
    taskManager.prototype.removeTask = function (taskName, taskID){
      if (this.tasks[taskID]){
        delete this.tasks[taskID];
      }        
    };
    return taskManager;
})();

Usage:

var taskManager1 = new taskManager();
taskManager1.addTask(a,b);
taskManager1.removeTask(a);
Sign up to request clarification or add additional context in comments.

3 Comments

The problem is I'd no longer have tasks grouped by name. Also I made a mistake in my original code showing taskID as the other piece of info I would be storing. In reality it would be a reference to the tasks dialog window, not an integer.
But I think you are right. tasks should be an object with properties that are arrays.
What is the point of the self-executing function you have it wrapped in? I don't see any particular benefit it is offering.
1

Arrays are meant to have numeric indexes and you can use .splice() to remove a numeric indexed item from an array. Non-numeric indexes aren't really in the array, they end up just being properties on the array object and they can be removed with the delete operator.

If you don't have numeric indexes, then you should be using an object and use a property to index each item. When doing it that way, you can use delete tasks[taskName] to remove a property from the object.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.