Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

205
Views
How to store data in map with dynamic input in javascript

I'm buliding my first projetct with node.js and trying to store user's input with Map here's my code and 2 incoming input:

const command = new Map()

let commadParam = ['userAccount', 'john'] // <- first input then it should be store for later use

let account = 'userAccount-'+ commadParam[1]
consolo.log(account) // 'userAccount-john'

commadParam = ['charItem','BowJohn'] // <- simulate second input

let char = 'charItem-' + commadParam[1]
consolo.log(char) // 'charItem-BowJohn'

command.set(account, account.split('-')[1])
command.set(char, char.split('-')[1])

console.log(command)// Map{'userAccount-john' => 'john', 'charItem-BowJohn' => 'BowJohn'}

it works fine when i literly put 2 input like example, but when i ran my app and type first input then after like 1sec i type second input it overwrite the first map

how do i store first input with Map for later use (like 1-3sec later) or should i just use DB for storing(like redis)? Thanks!

Here is full code:

const replyMsg = async (reqBody, res) => {
    const reqBodyMsg = reqBody.events[0].type
    const command = new Map()

    if (reqBodyMsg === 'message') {
       
        let commandParam = reqBody.events[0].message.text.split(' ')

        let account = 'userAccount-' + commandParam[1]
        let char = 'charItem-' + commandParam[1]

        command.set(account, account.split('-')[1])
        command.set(char, char.split('-')[1])
  
        let accountName = command.get(account)
        let charName = command.get(char)

        //input user account for character list
        if (commandParam[0] === 'userAccount') {

            const dataString = await getChar(reqBody, res, accountName)

            console.log(dataString)
        }

        //input character name to get character equipment
        if (commandParam[0] === 'charItem') {
            // accountName = data.key[1]
            // console.log(commandParam[0])
            // const charName = commandParam[1]

            const dataString = await getItem(reqBody, res, accountName, charName)
            console.log(dataString)
        }
    }
}

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Declare and initialize command outside the function. Then if commandParam[1] is different it should not overwrite the old values in the map.

I'm not sure why you need accountName and charName, they should be the same as commandParam[1].

const command = new Map()

const replyMsg = async(reqBody, res) => {
  const reqBodyMsg = reqBody.events[0].type

  if (reqBodyMsg === 'message') {

    let commandParam = reqBody.events[0].message.text.split(' ')

    let account = 'userAccount-' + commandParam[1]
    let char = 'charItem-' + commandParam[1]

    command.set(account, account.split('-')[1])
    command.set(char, char.split('-')[1])

    let accountName = command.get(account)
    let charName = command.get(char)

    //input user account for character list
    if (commandParam[0] === 'userAccount') {

      const dataString = await getChar(reqBody, res, accountName)

      console.log(dataString)
    }

    //input character name to get character equipment
    if (commandParam[0] === 'charItem') {
      // accountName = data.key[1]
      // console.log(commandParam[0])
      // const charName = commandParam[1]

      const dataString = await getItem(reqBody, res, accountName, charName)
      console.log(dataString)
    }
  }
}

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!