1

I have two kinds of arrays going on which are

var arrayAdmins = [{"firstName": "Admin", "lastName": "User", "email": "[email protected]","password": "admin"}];
var aBlocked = [{"firstName": "Blocked", "lastName": "User", "email": "[email protected]","password": "blocked"}]

To keep it simple for this question I have a button:

<input type="button"id="switch">

I then do a variable to target the button:

 var arraySwitch = document.getElementById('switch');

What I want to do is that when the button is clicked the user from the array called aBlocked over to arrayAdmins. So I'm hoping that from a certain admin panel, the admin can remove user from being blocked into being an admin.

7
  • Sorry I got a little confused, I was editing it and it was already fixed by someone else, thanks for letting me know.
    – SmalliSax
    Commented Sep 25, 2014 at 19:57
  • use another variable as a placeholder during the switch
    – Petro
    Commented Sep 25, 2014 at 19:58
  • What if I have 2 users and I only want to move one of them but not all at once ? I´m guessing the arrayAdmins.push(aBlocked) will move everything from the array.
    – SmalliSax
    Commented Sep 25, 2014 at 20:02
  • The only thing that worries me is how unsafe those password is
    – Endless
    Commented Sep 25, 2014 at 20:03
  • Hehe that was only for the questions purpose :)
    – SmalliSax
    Commented Sep 25, 2014 at 20:04

2 Answers 2

1

Your users should have an "id" so you can identify them in your array, I'll suppose in your case that the email is unique

var arrayAdmins = [{"firstName": "Admin", "lastName": "User", "email": "[email protected]","password": "admin"}];
var aBlocked = [{"firstName": "Blocked", "lastName": "User", "email": "[email protected]","password": "blocked"}]

var btnSwitch = document.getElementById('switch');
btnSwitch.onclick = SwitchClicked;

function SwitchClicked(){
    //Remove user from aBlocked
    var removedUser = RemoveUserByEmail(aBlocked, "[email protected]");

    //Add the removed user to arrayAdmins
    if(removedUser != null){
        arrayAdmins.push(removedUser);
    }
}

//Will remove the first user found with this email and returns it
function RemoveUserByEmail(arrayOfUsers, email){
    for(var i=0; i<arrayOfUsers.length; i++){
        if(arrayOfUsers[i].email === email){
            var removedUsers = arrayOfUsers.splice(i, 1);
            if(removedUsers.length > 0) return removedUsers[0];            
        }
    }

    return null;
}
0
1

Well, you need to have a way of targeting which user you want to move in your UI.

Then, it's just a matter of removing the user from one array and append it to the other. For exemple, if you want to remove the second user of aBlocked (if there was one) and add it to arrayAdmins you could do:

var unblockedUser = aBlocked.splice(1, 1)[0];
arrayAdmins.push(unblockedUser);

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.