1

I have a Project Management app buildt with JavaScript on the front end and PHP/MySQL on the back end.

I want to have an array holding all the Task ID's that a user is subscribed to notifications on.

When I show a Task record I want to set the status of the subscribe button based on if the user is subscribed already or not.

I also want the ability to add and remove task entrioes from the users array of subscriptions.

I am thinking something like this below would be a good start...

var userTaskSubscriptions = [{
  task_id: 1,
  subscribed: 'yes'
},
{
  task_id: 2,
  subscribed: 'yes'
},
{
  task_id: 3,
  subscribed: 'yes'
}]

In this example above I need some JavaScript code to allow me to check if user has an entry above for Task ID #2 and min this case they would have one.

I also need code to add and remove entries from it.

Any help here?

2
  • The question is not clear. Please, can you provide an example with input and desired output? Commented Dec 1, 2015 at 3:24
  • Are you using an ES6 environment? ie, is Map, Array.prototype.find, etc, available? Commented Dec 1, 2015 at 3:44

2 Answers 2

2

You can just loop trough it (by array size) and delete the index where the match is found.

function deleteTask(taskId) {
    for(var i = 0; i < userTaskSubscriptions.length; i++) {
        if(userTaskSubscriptions[i] == taskId) {
            userTaskSubscriptions.splice(i, 1);
        }
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0

Can you please try with this code: Check whether task_id is already subscribed or not, if it is 'yes', will delete from list and return subscribed value 'yes'. It will add new user to the list with two filled 'task_id' and subscribed value 'yes', which is default.

 var userTaskSubscriptions = [{
      task_id: 1,
      subscribed: 'yes'
    },
    {
      task_id: 2,
      subscribed: 'yes'
    },
    {
      task_id: 3,
      subscribed: 'yes'
    }];
    var subscribe=function(taskId){
     var _self=this;
      _self.taskId=taskId;
     var subscritpiton;
     $.each(userTaskSubscriptions,function(index,value){
       console.log(userTaskSubscriptions);
          if(taskId == value.task_id){

             userTaskSubscriptions.splice(index, 1);
             subscritpiton=value.subscribed;
             return false;
           }
          else{

             userTaskSubscriptions.push({task_id: _self.taskId,subscribed: 'yes'})
              return false;
          }
        })
    console.log(userTaskSubscriptions);
    return subscritpiton;
    };

subscribe(1); //call subscribe method here

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.