Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to append an object in javascript?

I have a question on how to build an object structure in java script.
I would like to have an object built in the following format:

select1Data = {
"some_email1@gmail.com" : "fname1 lname1",
"some_email2@gmail.com" : "fname2 lname2",
"some_email3@gmail.com" : "fname3 lname3"
};

The code I use is as follows:

$.ajax({
    method: "POST",
    url: "php/somescript.php",
    data: {"email": "some_email@gmail.com"},
}).done(function( data ) {
        var result = $.parseJSON(data);
        let select1Data = { null: null };
        if (result!== null){
                var len = result.length;
                for(var i=0; i<len; i++){
                        userName = result[i].imie + " " + result[i].nazwisko;
                        eMail = result[i].email;
                        let selectNewData = {eMail : userName};
                        Object.assign(select1Data, selectNewData);
                };
        
            }
        });

but this does not work. How can I append or build in another way this object?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

the line

let selectNewData = {eMail : userName};

will not do what you hope it do and is equivalent to {"eMail": userName}.

try:

let selectNewData = {};
selectNewData[eMail] = userName;
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading