• Empleos
  • Sobre nosotros
  • profesionales
    • Inicio
    • Empleos
    • Cursos y retos
  • empresas
    • Inicio
    • Publicar vacante
    • Nuestro proceso
    • Precios
    • Evaluaciones
    • Nómina
    • Blog
    • Comercial
    • Calculadora de salario

0

189
Vistas
get first 2 charater from an object using JS

i wanted represent first two letters of phone number,i want to get phone numbers first two characters. what i have tried is below Get first two of attribute and find target based on it.

    let object = [
        "key": {
               login-attempt: 1
               name: "ADMIN"
               phone: "0777123456"
            }]

what i have tried is below

object[0].substr(0,2); 
about 3 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

let test = {
        key: {
               "login-attempt": 1,
               name: "ADMIN",
               phone: "0777123456",
          }
     };
            
console.log(test.key.phone.substr(0,2)); 

about 3 years ago · Juan Pablo Isaza Denunciar

0

You're more likely to have an object like this:

let object = {
  login-attempt: 1
  name: "ADMIN"
  phone: "0777123456"
};

In this case, your code might be:

let prefix = object.phone.substr(0,2);

Or you might have an array of objects:

let object = [
  {
    login-attempt: 1
    name: "ADMIN"
    phone: "0777123456"
  },
  {
    login-attempt: 1
    name: "CLERK"
    phone: "2222222222"
  }
];

You can get the 1st two characters of the second element like this:

let prefix = object[1].phone.substr(0,2);

If the element was nested within "key":

let object = {
  key: {
    login-attempt: 1
    name: "ADMIN"
    phone: "0777123456"
  }
};

let prefix = object.key.phone.substr(0,2);

And so on...

about 3 years ago · Juan Pablo Isaza 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 Nuestro proceso Comercial
Legal
Términos y condiciones Política de privacidad
© 2025 PeakU Inc. All Rights Reserved.

Andres GPT

Recomiéndame algunas ofertas
Necesito ayuda