我的代码不起作用。我需要反馈,并且需要一种有效的方法来返回与传递给函数的ID匹配的名字和姓氏的缩写。
var contactList = [
{
"id": 'e3d46de8c7194cb1a32275195c15dc07',
"name": 'Niels Bohr',
"num_messages": 62,
},
{
'id': '7064c3f9c99743b2838bbd8eacafe0d6',
"name": 'Max Planck',
"num_messages": 15,
},
{
"id": 'b19e575a0d3f4151a1391452d8a47a44',
"name": 'Jane Goodall',
"num_messages": 20,
},
{
"id": '17d9d0908f454253b5337e8c1ef4b564',
"name": "Caroline Herschel",
"num_messages": 3,
},
]
function getInitials(id){
// find the user object
for (let u of contactList) {
if (u["id"] == id){
var initials = ''
names = u.name.split(' ')
for(var i; i < names.length; i++){
initials += names[i][0]
}
// return the initials
return initials
}
}
}
答案 0 :(得分:2)
首先使用 find() 找到与id
匹配id
的商品。然后split()
及其name
并将其映射到其name[0]
然后是join()
var contactList = [
{
"id": 'e3d46de8c7194cb1a32275195c15dc07',
"name": 'Niels Bohr',
"num_messages": 62,
},
{
'id': '7064c3f9c99743b2838bbd8eacafe0d6',
"name": 'Max Planck',
"num_messages": 15,
},
{
"id": 'b19e575a0d3f4151a1391452d8a47a44',
"name": 'Jane Goodall',
"num_messages": 20,
},
{
"id": '17d9d0908f454253b5337e8c1ef4b564',
"name": "Caroline Herschel",
"num_messages": 3,
},
]
function getInitials(id){
let obj = contactList.find(item => item.id === id);
return obj && obj.name.split(' ').map(a => a[0]).join('');
}
console.log(getInitials('7064c3f9c99743b2838bbd8eacafe0d6'))
console.log(getInitials('17d9d0908f454253b5337e8c1ef4b564'))
console.log(getInitials('b19e575a0d3f4151a1391452d8a47a44'))
console.log(getInitials('0'))
答案 1 :(得分:1)
您可以找到对象,获取名称并创建其初始内容。
function getInitials(array, id) {
var temp = array.find(o => o.id === id);
return temp && temp.name.split(' ').map(([c]) => c).join('');
}
var contactList = [{ id: 'e3d46de8c7194cb1a32275195c15dc07', name: 'Niels Bohr', num_messages: 62 }, { id: '7064c3f9c99743b2838bbd8eacafe0d6', name: 'Max Planck', num_messages: 15 }, { id: 'b19e575a0d3f4151a1391452d8a47a44', name: 'Jane Goodall', num_messages: 20 }, { id: '17d9d0908f454253b5337e8c1ef4b564', name: "Caroline Herschel", num_messages: 3 }];
console.log(getInitials(contactList, 'b19e575a0d3f4151a1391452d8a47a44'));
console.log(getInitials(contactList, '000'));
答案 2 :(得分:0)
您可以尝试以下代码
var contactList = [
{
"id": 'e3d46de8c7194cb1a32275195c15dc07',
"name": 'Niels Bohr',
"num_messages": 62,
},
{
'id': '7064c3f9c99743b2838bbd8eacafe0d6',
"name": 'Max Planck',
"num_messages": 15,
},
{
"id": 'b19e575a0d3f4151a1391452d8a47a44',
"name": 'Jane Goodall',
"num_messages": 20,
},
{
"id": '17d9d0908f454253b5337e8c1ef4b564',
"name": "Caroline Herschel",
"num_messages": 3,
},
]
//here constant is your id which you passed in if condition
var str = contactList.find(x => x.id =='17d9d0908f454253b5337e8c1ef4b564').name.split(' ');
var result =""
for(var s of str)
result += s.substr(0, 1)
console.log("Initals of \""+str+"\" is " + result);
答案 3 :(得分:0)
尝试一下,可能是您需要的吗?
var contactList = [
{
"id": 'e3d46de8c7194cb1a32275195c15dc07',
"name": 'Niels Bohr',
"num_messages": 62,
},
{
'id': '7064c3f9c99743b2838bbd8eacafe0d6',
"name": 'Max Planck',
"num_messages": 15,
},
{
"id": 'b19e575a0d3f4151a1391452d8a47a44',
"name": 'Jane Goodall',
"num_messages": 20,
},
{
"id": '17d9d0908f454253b5337e8c1ef4b564',
"name": "Caroline Herschel",
"num_messages": 3,
},
]
function getInitials(id){
// find the user object
for (let u of contactList) {
if (u["id"] == id){
var initials = ''
names = u.name.split(' ')
console.log(names.length);
for(var i=0; i< names.length; i++){
initials+=' ' + names[i].charAt(0);
}
// return the initials
console.log(initials, 'pop');
return initials
}
}
}
getInitials('17d9d0908f454253b5337e8c1ef4b564');
答案 4 :(得分:0)
使用Array.reduce
和Array.find
的一种简单的方法
如果没有id,此函数将返回undefined。
var contactList = [
{
"id": 'e3d46de8c7194cb1a32275195c15dc07',
"name": 'Niels Bohr',
"num_messages": 62,
},
{
'id': '7064c3f9c99743b2838bbd8eacafe0d6',
"name": 'Max Planck',
"num_messages": 15,
},
{
"id": 'b19e575a0d3f4151a1391452d8a47a44',
"name": 'Jane Goodall',
"num_messages": 20,
},
{
"id": '17d9d0908f454253b5337e8c1ef4b564',
"name": "Caroline Herschel",
"num_messages": 3,
},
]
function getInitials(id){
//using .find to find the element in the array
const contact = contactList.find(contact => contact.id === id)
//cross checking if id is present and for that id name key is present and then using reduce function to get initials.
return contact && contact.name && contact.name.split(' ').reduce((initial, name) => initial + name[0], '')
}
console.log(getInitials('b19e575a0d3f4151a1391452d8a47a44')) //JG
console.log(getInitials('random')) //undefined
答案 5 :(得分:0)
这是在单个操作链中使用filter
,map
,shift
和正则表达式的简洁方法。该技术可以在任意数量的空格中使用,请参见下面的示例,如果找不到联系人ID,则会返回undefined
。
首先,对数组进行过滤以获取正确的ID,这将返回一个数组(可以为空),您可以在该数组上进行映射以提取带有String.match
,正则表达式和Array.join
的首字母缩写。然后,您将第一个元素移出以获得结果,如果没有元素,shift
不会引发错误,只会返回undefined
。
这里有两种可能的用于提取首字母的正则表达式:
/\b(\w)/g
/(?<=(\s+|^))(\w)/g
第一个正则表达式使用\b
来匹配单词边界,而\w
将匹配任何单词字符,因此正则表达式\b(\w)
将捕获每个单词的第一个字符。当名称用短划线分隔时,此正则表达式将失败,因为短划线将视为单词分隔符。
第二个正则表达式将使用(?<=(\s+|^))
后的正斜线匹配每个第一个单词字符,以检查空格或字符串的开头。
const contactList = [{
"id": 'e3d46de8c7194cb1a32275195c15dc07',
"name": ' Niels Bohr',
"num_messages": 62,
}, {
'id': '7064c3f9c99743b2838bbd8eacafe0d6',
"name": 'Max Planck ',
"num_messages": 15,
}, {
"id": 'b19e575a0d3f4151a1391452d8a47a44',
"name": ' Jane Goodall',
"num_messages": 20,
}, {
"id": '17d9d0908f454253b5337e8c1ef4b564',
"name": "Caroline Herschel",
"num_messages": 3,
}];
function getInitials(id) {
return contactList
.filter(contact => contact.id === id)
.map(contact => contact.name.match(/(?<=(\s+|^))(\w)/g).join(''))
.shift();
}
console.log(getInitials('17d9d0908f454253b5337e8c1ef4b564'));
console.log(getInitials('b19e575a0d3f4151a1391452d8a47a44'));
console.log(getInitials('7064c3f9c99743b2838bbd8eacafe0d6'));
console.log(getInitials('e3d46de8c7194cb1a32275195c15dc07'));
console.log(getInitials('123'));
答案 6 :(得分:0)
You have bag :
` for(var i; i < names.length; i++){
initials += names[i][0]
} `
1.i have to be initelased var i = 0;
除此之外,您的代码还在工作