Company logo
  • Empleos
  • Bootcamp
  • Acerca de nosotros
  • Para profesionales
    • Inicio
    • Empleos
    • Cursos y retos
    • Preguntas
    • Profesores
    • Bootcamp
  • Para empresas
    • Inicio
    • Nuestro proceso
    • Planes
    • Pruebas
    • Nómina
    • Blog
    • Calculadora

0

85
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); 
7 months 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)); 

7 months 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...

7 months 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 empleo Planes Nuestro proceso Comercial
Legal
Términos y condiciones Política de privacidad
© 2023 PeakU Inc. All Rights Reserved.