0

So I am not the best at writing functions so I am having a hard time wrapping a head around this. So I am trying to create a function that traverses through array of objects, and stops once it reads the name that is given it must return the number of the that person.

const array = [{name: 'Ann', phone: '575.580.1400', role: 'Developer'},
               {name: 'Ben', phone: '575.641.4041', role: 'Manager'},
               {name: 'Clara', phone: '512.717.5690', role: 'Developer'}];

const getNumber = (person, book ) => {
      for (var x of book ) {
          if( x == person) {
             return number;}
     return ('Not found'); 
     }
}

I know I am missing how to call in the number, but I just can't think of how to do it.

2
  • x is an object from your array, you can access the name key using x.name, same with the number: x.number Commented Sep 12, 2020 at 4:07
  • let foundPerson = array.filter((person) => person.name == name); Commented Sep 12, 2020 at 4:25

3 Answers 3

1

First you need to access the key inside the object and return ('Not found'); is not in the right place. Secondly use === instead of ==.In your code if the function will return in the fist iteration only. Because if you search for Clara and in the if condition Ann will not be equal to Clara so it will return Not Found and will not iterate the remaining array

const array = [{
    name: 'Ann',
    phone: '575.580.1400',
    role: 'Developer'
  },
  {
    name: 'Ben',
    phone: '575.641.4041',
    role: 'Manager'
  },
  {
    name: 'Clara',
    phone: '512.717.5690',
    role: 'Developer'
  }
];

const getNumber = (person, book) => {
  for (var x of book) {
    if (x.name === person) {
      return x.phone;
    }
  }
  return ('Not found');
}
console.log(getNumber('Clara', array))

Alternatively you can also use array methods like find or filter

const array = [{
    name: 'Ann',
    phone: '575.580.1400',
    role: 'Developer'
  },
  {
    name: 'Ben',
    phone: '575.641.4041',
    role: 'Manager'
  },
  {
    name: 'Clara',
    phone: '512.717.5690',
    role: 'Developer'
  }
];

const num = array.find(item => item.name === 'Clara').phone;
console.log(num)

Sign up to request clarification or add additional context in comments.

Comments

0

Below code helps to iterate through a JSON object and print the required value. You can use the required conditions in IF and print the desired values accordingly.

 var json = [{name: 'Ann', phone: '575.580.1400', role: 'Developer'},
           {name: 'Ben', phone: '575.641.4041', role: 'Manager'},
           {name: 'Clara', phone: '512.717.5690', role: 'Developer'}];

for (var key in json) {
   if (json.hasOwnProperty(key)) {
   if(json[key].name=='Ben')
      
      console.log(json[key].phone);
   }
}

Comments

0

Try this

I am using some instead of foreach or other loops. some() method executes the callback function once for each element present in the array until it finds the one where callback returns a truthy value.

const getNumber = (person, book ) => { 
      let pnumber;
      book.some((item) => {
        if(item.name.toLowerCase()===person.toLowerCase()){
            pnumber = item.phone;
            return true;
             }
         })
      return pnumber ? pnumber: "Not Found" ;
     }

you can call it this way

getNumber('clara',array)
"512.717.5690"

getNumber('ben1',array)
"Not Found"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.