javascript - 如何对姓氏进行排序、设置电话号码格式以及返回包含对象数组中的值的字符串?

标签 javascript arrays javascript-objects

我正在用 JS 解决这个玩具问题,但我已经被困了几个小时不知道如何继续。

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.

下面是我的代码。我创建了一个函数,能够创建一个包含名称值的数组,将电话号码格式化为一个数组,按姓氏对名称数组进行排序,但我无法弄清楚将所有这些绑定(bind)起来以返回字符串的逻辑。我相信它需要一个 if 语句来表示性别值。谁能帮忙逻辑一下回答这个问题?这是我的代码。感谢您的阅读。

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

最佳答案

好吧,我尝试了一下,我采取了与你不同的方式,并且我没有进行最终的格式化,但我相信我已经做到了。对于将其转换为字符串的最后一步,我只需循环最终对象并通过将所有属性添加在一起来创建一个字符串:

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

这是我操作数组的代码:

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);
});

关于javascript - 如何对姓氏进行排序、设置电话号码格式以及返回包含对象数组中的值的字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36464165/

相关文章:

javascript - 对象数组,输入对象中每个键的对象

javascript - Angular 2 访问 REST API 错误 - 没有 Http 提供程序

javascript - Sencha Touch - 导航 View 内的 NestedList

javascript - 如果 jQuery 符合 AMD 标准,为什么它会创建全局变量 $?

php - 在 Laravel 中创建友好的 url

php - 通过 array_push 计算行数

javascript - 如何使用 Bluebird Promises 延迟迭代元素?

arrays - 提取数组中的 tableViewCell 宽度?

javascript - 如何根据多个条件对对象数组重新排序?

javascript - 如何为已定义的 JavaScript 对象定义 getter 属性?