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

253
Visualizações
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 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

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

about 4 years ago · Juan Pablo Isaza Relatório

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