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 = {
"[email protected]" : "fname1 lname1",
"[email protected]" : "fname2 lname2",
"[email protected]" : "fname3 lname3"
};
The code I use is as follows:
$.ajax({
method: "POST",
url: "php/somescript.php",
data: {"email": "[email protected]"},
}).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?
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;