0

I need a way to implement the following C++ in javascript.

struct login
{
   char username[14];
   char password[7];
   char verificationCode[11];
   int timesLoged;
}; 

login allLogins[] = 
{
      {"none", "", "",0},
      {"user1", "159951", "",0},
      {"user2", "123321", "",0},
      {"user3", "456654", "",0}
};

I would like to be able to refer to each member in similar/same fashion.

String currentLoginName = allLogins[3].username
8
  • Uhh, this isn't JavaScript. Commented Sep 16, 2017 at 10:28
  • nop, its in C++ and wont run in Javascript Commented Sep 16, 2017 at 10:31
  • 1
    Please try something before asking. Commented Sep 16, 2017 at 10:31
  • 1
    @Vangde if you can do this in C++ it's almost impossible not to be able to figure out how to do it in JavaScript. Commented Sep 16, 2017 at 10:34
  • 1
    Well the tho languages differ quite a lot in syntax and im not very familiar with javascript. For exampe i found out that there is no structs in Js. Commented Sep 16, 2017 at 10:36

2 Answers 2

1
function User([username,password,verification,times]){
  return {
   username,
   password,
   verification,
   times
  };
}

const allLogins = [
 ["user","test","1234",0],
 ["user2", "test2","1235", 0]
].map(User);

One could map a 2d array to an array of Users.

Sign up to request clarification or add additional context in comments.

Comments

1

Code by Rahul Jain.

class Login {
  constructor(username, password, verificationCode, timesLoged) {
    this.username = username;
    this.password = password;
    this.verificationCode = verificationCode;
    this.timesLoged = timesLoged;
  }
}

var allLogins = [
    new Login("none", "", "",0),
  new Login("user1", "159951", "",0),
  new Login("user2", "123321", "",0),
  new Login("user3", "456654", "",0)
]

alert(allLogins[3].password);

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.