简体   繁体   中英

How do I sort the last names , format phone numbers, and return a string containing values from an array of objects?

I'm working on this toy problem in JS, but I've been stuck for hours on how to proceed.

Define an object constructor People that contains the values: full name, email, phone number, age, and gender.

Create four new People(names & values are up to you) and assign them to an array.

  • Phone number and age should be numbers.
  • At least one full name should contain a middle name.

Then do the following:

1- Split the full name values into an array. 2- Format the phone number: 1(123)456-7898 3- Sort contacts by last name. 4- Make a string of each object and log it to the console like so:

Mr/Ms lastname, firstname | email | phone number

a: Spacing should be the same for and fit all values. | b: Title, name, and phone number should be left justified. | c: Email should be right justified.

Below is my code. I made a function that is able to create an array with the name values, formatted the phone numbers into an array, sorted the names array by last name, but I can't figure out the logic to tie all this back to return a string. I believe it requires an if statement for the gender value. Can anybody help with the logic to answer this question? Here is my code. Thanks for reading.

function People(name, email, age, phone, gender){
  this.fullname = name;
  this.email = email;
  this.age = age;
  this.phone = phone;
  this.gender = gender;
}

// Assigning four new people obj to an array

var arr = [
  new People("John Doe", "jdoe@aol.com", 32, 15555555555, "male"),
  new People("Jenny Craig", "jcraig@netzero.com", 28, 19255555555,   "female"),
  new People("Bill Clinton", "bill.clinton@pres.gov", 59, 13105551234, "male"),
  new People("George Washington Bush", "gw.bush@gmail.com", 63, 15103456432, "male")
];


// Loop through array to access each obj fullname prop to split into array.

function nmStr(arr){
  var name = [];
  var num = [];
  arr.forEach(function(elem){
    name.push(elem.fullname.split(" ").reverse());
  });
  name.sort();
  arr.forEach(function(numb){
    numb.push(numb.phone.toString().substr(0, 1) + "(" +
    numb.phone.toString().substr(1, 3) + ")" +
    numb.phone.toString().substr(4, 3) + "-" +
    numb.phone.toString().substr(7, 4));
  });
  // console.log(name);
  // console.log(num);
}

nmStr(arr);

Ok I took a stab at it, I went a bit of a different way than you and I didn't do the final formatting, but I have it up to there I believe. For the last step to turn it into a string, I just loop over the final object and make a string by adding all the properties together:

JSBin... Click, it will auto log my results

Here is my code to manipulate the array:

function phonenumbermaker(num) {
  var str = num.toString();
  var re = /\(?(\d{4})\)?[- ]?(\d{3})[- ]?(\d{4})/g; 
  var subst = '$1 $2-$3'; 
  var result = str.replace(re, subst); 
  return  result.substring(0, 1) + '(' +  result.substring(1,4) + ')' + result.substring(4);
}
var updated = arr.map(function(person) {
  var nameArray = person.fullname.split(' ');
  return {
    gender: (person.gender === 'male' ? 'Mr.' : 'Mrs.'),
    lastname: nameArray.pop(),
    restof : nameArray.join(' '),
    email: person.email,
    phone: phonenumbermaker(person.phone)
  };
}).sort(function(a, b) {
    if (a.lastname > b.lastname) return 1;
    if (a.lastname < b.lastname) return -1;
    return 0;
}).map(function(p) {
  return p.gender + ' ' + p.lastname + ', ' +  p.restof + ' | ' + p.email + ' | ' + p.phone; 
});

// this part is just to log:
updated.forEach(function(finalThing){

  console.log(finalThing);
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM