Company logo
  • Jobs
  • Bootcamp
  • About Us
  • For professionals
    • Home
    • Jobs
    • Courses
    • Questions
    • Teachers
    • Bootcamp
  • For business
    • Home
    • Our process
    • Plans
    • Assessments
    • Payroll
    • Blog
    • Calculator

0

84
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post job Plans Our process Sales
Legal
Terms and conditions Privacy policy
© 2023 PeakU Inc. All Rights Reserved.