简体   繁体   中英

how can you make an array save as a new object each time a function is executed and not overwrite my old array

I'm trying to make a simple register and log in system but I can only register 1 new username and password and the next time it is overwritten. I know the [2] after objpeople means it only writes on that line but what do put in to make it create a new object everytime?

var objpeople = [
            {
                username: "adam",
                password: "coll",
                email: "neilcoll@mail.com"
            },
            {
                username: "jack",
                password: "mc",
                email: "jackmcgmail.com"
            }
        ];


    function register()
    {
        var Rusername = document.getElementById("Rname").value  
        var Rpassword = document.getElementById("Rpassword").value
        var Remail = document.getElementById("Remail").value
        var info = {};


        objpeople.push(info);
        objpeople[2].username=Rusername;
        objpeople[2].password=Rpassword;
        objpeople[2].email=Remail;


        let convobjpeople = JSON.stringify(objpeople)
        localStorage.people = convobjpeople

        document.getElementById("logbtn").innerHTML = "Cart";
        document.getElementById("logbtn").href="cart.html";
        localStorage.setItem('logged', 'true');
        return ;    
    }

edit: your code implementation

    var objpeople = [
            {
                username: "neil",
                password: "coll",
                email: "neilcoll@gmail.com"
            },
            {
                username: "jack",
                password: "mc",
                email: "jackmc@gmail.com"
            }
        ];



    function register()
    {
        let my_object =  {
                username: "",
                password: "",
                email: ""
            }
            var Rusername = document.getElementById("Rname").value  
            var Rpassword = document.getElementById("Rpassword").value
            var Remail = document.getElementById("Remail").value

            my_object.username=Rusername;
            my_object.password=Rpassword;
            my_object.email=Remail;

            objpeople.push(my_object)

            let convobjpeople = JSON.stringify(objpeople)
            localStorage.people = convobjpeople

            document.getElementById("logbtn").innerHTML = "Cart";
            document.getElementById("logbtn").href="cart.html";
            localStorage.setItem('logged', 'true');
            return ;
    }

Are you trying to create objects and push them into an array?

Just initiate an object

let my_object =  {
                username: "",
                password: "",
                email: ""
            }

and when you want to assign values

var Rusername = document.getElementById("Rname").value  
var Rpassword = document.getElementById("Rpassword").value
var Remail = document.getElementById("Remail").value

 my_object.username=Rusername;
 my_object.password=Rpassword;
 my_object.email=Remail;

and finally

objpeople.push(my_object)

Edit: It shouldn't really be behaving like this but either ways you could just create a replica object that is not passed on to a server. So for example create a objpeople2 and instead of objpeople.push(my_object) just do objpeople2.push(my_object) and then objpeople = objpeople2 and finally

let convobjpeople = JSON.stringify(objpeople) . This way objpeople2 is kept as it is.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM