Home > Blockchain >  How to initialize array or object?
How to initialize array or object?

Time:08-28

I don't know how to initialize an object or array like this:

contacts:

- messages:
-- 'Hello, man'
-- 'What is going on'
-- 'idk i will call you later'

- usernames:
-- 'Antonio Capuccino'
-- 'Karlo Frunze'
-- 'Punisher3000'

And that is how I want to find this data:

  • Input: contacts.messages[0]
  • Output: 'Hello, man'

Or:

  • Input: contacts.usernames[2]
  • Output: 'Punisher3000'

CodePudding user response:

Create an object like this

const contacts = {
  messages: [
    "Hello, man",
    "What is going on",
    "idk i will call you later",
  ],
  usernames: [
    "Antonio Capuccino",
    "Karlo Frunze",
    "Punisher3000",
  ],
};

console.log(contacts.messages[0]);
console.log(contacts.usernames[2]);

CodePudding user response:

You could do something like this:

const contacts = {
  messages: ["Hello, man", "What is going on", "idk i will call you later"],
  usernames: ["Antonio Capuccino", "Karlo Frunze", "Punisher3000"],
};

console.log("Example One: "   contacts.messages[0])
console.log("Example Two: "   contacts.usernames[2]) 

  • Related