Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

126
Visualizações
how to conver the array of undefined to a array of string

I trying to pass the array value from frontend to the backend, while transmitting the value i facing some errors

this is my response data

{
  sender: 'venkat',
  numbers: '[919361667266, 919361667266, 919361667266, 919361667266]',
  message: 'hiii'
}

I need to convert this number data this '[919361667266, 919361667266, 919361667266, 919361667266]'

to

'["919361667266", "919361667266", "919361667266", "919361667266"]'

the error i faced while parsing

const numbers = JSON.parse(req.body.numbers);


(node:27058) UnhandledPromiseRejectionWarning: SyntaxError: Unexpected token @ in JSON at position 13
    at JSON.parse (<anonymous>)
    at /Users/
about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

You are not sending proper JSON. JSON used double quotes around the key and string values

{
  "sender": "venkat",
  "numbers": "[919361667266, 919361667266, 919361667266, 919361667266]",
  "message": "hiii"
}

Alternatively, you could stringify your object

const payload = JSON.stringify({
  sender: 'venkat',
  numbers: '[919361667266, 919361667266, 919361667266, 919361667266]',
  message: 'hiii'
});
about 4 years ago · Juan Pablo Isaza Relatório

0

After JOSN.parse the numbers param be an array of numbers, this is the case in the response because the server is sending an array of numbers. So you can either handle it in the server side or the client side

At Server Side

Before sending response stringily the values in the numbers array

numbers.map(val => val.toString())

OR

At Client Side

Parse it and convert it to string

JSON.parse(numbers).map(val => val.toString())

about 4 years ago · Juan Pablo Isaza Relatório

0

Frontend is sending '[919361667266, 919361667266, 919361667266, 919361667266]'

What you can do is convert that string to an array of strings: response.numbers.split(/\D+/).filter(s => !!s)

Code:

const response = {
  sender: 'venkat',
  numbers: '[919361667266, 919361667266, 919361667266, 919361667266]',
  message: 'hiii'
}
const numbers = JSON.stringify(response.numbers.split(/\D+/).filter(s => !!s))
const str = JSON.stringify({
  ...response,
  numbers
})
const obj = JSON.parse(str)

console.log('Result Str: ', str)
console.log('Result Obj: ', obj)

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda