Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

378
Vistas
How to convert char to int in array (JavaScript)

How to change char value in array to int value? and detect first element and second element in array

arr = [a,a,a,b,b,a,b]

first element = a

second element = b

a = 1, b = 2

My expected output

arr = [1,1,1,2,2,1,2]
about 4 years ago · Santiago Trujillo
3 Respuestas
Responde la pregunta

0

Something like this should work (I used NodeJs console):

> let a = ['a','a','a','b','b','a','b']
undefined
> a
[
  'a', 'a', 'a',
  'b', 'b', 'a',
  'b'
]
> var result = []
> for (var i = 0; i < a.length; i++){
... result.push(a[i].charCodeAt(0))
... }
7
> result
[
  97, 97, 97, 98,
  98, 97, 98
]
about 4 years ago · Santiago Trujillo Denunciar

0

You can do it like so.

You can only get the char code of an index in a string. Since we have an char array we can alsways get the index of 0. The smallest charcode is of A which is 65. In this example I transform a to A which resolves to 65 then I remove 64 since a should be 1. Consecutively b is mapped to B which is 65 and resolves to 2 since 64 is removed

const arr = ['a','a','a','b','b','a','b']


const res = arr.map(char => char.toUpperCase().charCodeAt(0) - 64)
console.log(res)

about 4 years ago · Santiago Trujillo Denunciar

0

let a =1;
let b =2;
let  arr = [a,a,a,b,b,a,b]

arr = arr.map((x) => {
    return x.toString()
})

console.log(arr)
about 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda