循环由于某种原因卡住了,最后 returns undefined
Loop is stuck for some reason and then finally returns undefined
这是我要解决的问题:
给定:包含名称散列的数组
Return:一个字符串,格式为以逗号分隔的名称列表,但最后两个名称应以“&”符号分隔。
示例:
list([ {name: 'Bart'}, {name: 'Lisa'}, {name: 'Maggie'} ])
// returns 'Bart, Lisa & Maggie'
list([ {name: 'Bart'}, {name: 'Lisa'} ])
// returns 'Bart & Lisa'
list([ {name: 'Bart'} ])
// returns 'Bart'
list([])
// returns ''
注意:所有哈希值都经过预先验证,并且只会包含 A-Z、a-z、'-' 和 '.
这是我的代码:
var finalName;
var notFinal;
function list(names){
var finalNames = names.forEach(returnNames);
console.log(typeof finalNames);
function returnNames() {
for (var i = 0; i<names.length; i++) {
var nameValue = Object.keys(names[i]).map(key => names[i][key])
}
}
for(var i = 0; i<finalNames.length; i++) {
if (finalNames.length / i == 1) {
finalName = "& " + finalNames[i];
}
else {
notFinal = finalNames[i] + ", ";
}
}
console.log(notFinal + finalName);
}
list([{name: 'Bart'},{name: 'Lisa'},{name: 'Maggie'},{name: 'Homer'},{name: 'Marge'}])
一直卡在循环里,最后报错:
TypeError: Cannot read property 'length' of undefined
at list
at /home/codewarrior/index.js:30:1
at Object.handleError
<anonymous>
我该如何解决?
那是因为 forEach 没有 return 任何东西,请尝试使用映射函数。
var finalNames = names.map(returnNames);
正如他们已经指出的那样,Array.prototype.forEach
returns undefined
。相反,您可以为此使用 .map
,修改您的 returnNames
函数
var finalName;
var notFinal;
function list(names){
// Changed .forEach with .map
var finalNames = names.map(returnNames);
console.log(typeof finalNames);
function returnNames(person) {
// If you only need to get the object values, use Object.values instead of Object.keys
return Object.values(person);
}
for(var i = 0; i < finalNames.length; i++) {
// Added + 1 because i could never be equal to the array length
// Note that you'll need to make 1 or 2 more changes before this code works as expected
if (finalNames.length / (i + 1) == 1) {
finalName = "& " + finalNames[i];
}
else {
notFinal = finalNames[i] + ", ";
}
}
console.log(notFinal + finalName);
}
list([{name: 'Bart'},{name: 'Lisa'},{name: 'Maggie'},{name: 'Homer'},{name: 'Marge'}])
您可以像下面这样简化您的代码。
function list(names){
//Determine length of array (= number of names)
const len = names.length;
//Use a simple for loop to go through the array
let newNames = "";
for (i=0; i<len; i++) {
newNames += names[i].name;
if ( i<len-2 ) newNames += ", "
else if ( i<len-1 ) newNames += " & "
}
console.log(newNames);
}
list([{name: 'Bart'},{name: 'Lisa'},{name: 'Maggie'},{name: 'Homer'},{name: 'Marge'}])
list([{name: 'Bart'}, {name: 'Lisa'}]);
list([{name: 'Bart'}]);
我已使用 reduce
来找到最终字符串以根据您的要求显示名称。希望这有帮助。
formatString = (list) => {
let length = (list || []).length;
return list.reduce((result, obj, index) => {
result += obj.name || "";
if(index + 1 === length - 1) {
result += " & ";
} else if(index !== length - 1) {
result += ", ";
}
return result;
}, "")
}
let list = [{"name":"Bart"},{"name":"Lisa"},{"name":"Maggie"},{"name":"Homer"},{"name":"Marge"}]
console.log(formatString(list))
list = [ {name: 'Bart'}, {name: 'Lisa'} ]
console.log(formatString(list))
list = [ {name: 'Bart'} ]
console.log(formatString(list))
list = []
console.log(formatString(list))
您的代码有两个问题。
如果你通过你的代码
function list(names){
var finalNames = names.forEach(returnNames);
console.log(typeof finalNames);
function returnNames() {
for (var i = 0; i<names.length; i++) {
var nameValue = Object.keys(names[i]).map(key => names[i][key])
}
}
在 forEach
中,您将 returnNames
作为回调传递。 forEach
将遍历数组的每个元素。将每个元素传递给提供的回调并执行它。
如您所见,在回调中,您再次使用 for 循环遍历整个列表。如此有效,对于每个元素,您再次遍历整个列表,将复杂性增加到 O(n2)
第二个问题,正如其他答案所指出的,您没有从 forEach
回调中返回任何内容。因此,finalNames
的值变为 undefined
。所以,当你 运行 这个语句
finalNames.length / i == 1
length
属性无法读取
function returnNameListString(namelist){
if (namelist.length){
return
}else if(namelist.length===1){
return toString(namelist)
}else{
nameString = namelist[0]
for(var i=1; i<(namelist.length-1);i++){
nameString = nameString + ", " + namelist[i]
}
nameString = nameString + " & " + namelist[namelist.length-1]
return nameString
}
}
试试这个!将名称声明为列表 ["tom"、"dick"、"harry"]
这是我要解决的问题: 给定:包含名称散列的数组
Return:一个字符串,格式为以逗号分隔的名称列表,但最后两个名称应以“&”符号分隔。
示例:
list([ {name: 'Bart'}, {name: 'Lisa'}, {name: 'Maggie'} ])
// returns 'Bart, Lisa & Maggie'
list([ {name: 'Bart'}, {name: 'Lisa'} ])
// returns 'Bart & Lisa'
list([ {name: 'Bart'} ])
// returns 'Bart'
list([])
// returns ''
注意:所有哈希值都经过预先验证,并且只会包含 A-Z、a-z、'-' 和 '.
这是我的代码:
var finalName;
var notFinal;
function list(names){
var finalNames = names.forEach(returnNames);
console.log(typeof finalNames);
function returnNames() {
for (var i = 0; i<names.length; i++) {
var nameValue = Object.keys(names[i]).map(key => names[i][key])
}
}
for(var i = 0; i<finalNames.length; i++) {
if (finalNames.length / i == 1) {
finalName = "& " + finalNames[i];
}
else {
notFinal = finalNames[i] + ", ";
}
}
console.log(notFinal + finalName);
}
list([{name: 'Bart'},{name: 'Lisa'},{name: 'Maggie'},{name: 'Homer'},{name: 'Marge'}])
一直卡在循环里,最后报错:
TypeError: Cannot read property 'length' of undefined
at list
at /home/codewarrior/index.js:30:1
at Object.handleError
<anonymous>
我该如何解决?
那是因为 forEach 没有 return 任何东西,请尝试使用映射函数。
var finalNames = names.map(returnNames);
正如他们已经指出的那样,Array.prototype.forEach
returns undefined
。相反,您可以为此使用 .map
,修改您的 returnNames
函数
var finalName;
var notFinal;
function list(names){
// Changed .forEach with .map
var finalNames = names.map(returnNames);
console.log(typeof finalNames);
function returnNames(person) {
// If you only need to get the object values, use Object.values instead of Object.keys
return Object.values(person);
}
for(var i = 0; i < finalNames.length; i++) {
// Added + 1 because i could never be equal to the array length
// Note that you'll need to make 1 or 2 more changes before this code works as expected
if (finalNames.length / (i + 1) == 1) {
finalName = "& " + finalNames[i];
}
else {
notFinal = finalNames[i] + ", ";
}
}
console.log(notFinal + finalName);
}
list([{name: 'Bart'},{name: 'Lisa'},{name: 'Maggie'},{name: 'Homer'},{name: 'Marge'}])
您可以像下面这样简化您的代码。
function list(names){
//Determine length of array (= number of names)
const len = names.length;
//Use a simple for loop to go through the array
let newNames = "";
for (i=0; i<len; i++) {
newNames += names[i].name;
if ( i<len-2 ) newNames += ", "
else if ( i<len-1 ) newNames += " & "
}
console.log(newNames);
}
list([{name: 'Bart'},{name: 'Lisa'},{name: 'Maggie'},{name: 'Homer'},{name: 'Marge'}])
list([{name: 'Bart'}, {name: 'Lisa'}]);
list([{name: 'Bart'}]);
我已使用 reduce
来找到最终字符串以根据您的要求显示名称。希望这有帮助。
formatString = (list) => {
let length = (list || []).length;
return list.reduce((result, obj, index) => {
result += obj.name || "";
if(index + 1 === length - 1) {
result += " & ";
} else if(index !== length - 1) {
result += ", ";
}
return result;
}, "")
}
let list = [{"name":"Bart"},{"name":"Lisa"},{"name":"Maggie"},{"name":"Homer"},{"name":"Marge"}]
console.log(formatString(list))
list = [ {name: 'Bart'}, {name: 'Lisa'} ]
console.log(formatString(list))
list = [ {name: 'Bart'} ]
console.log(formatString(list))
list = []
console.log(formatString(list))
您的代码有两个问题。 如果你通过你的代码
function list(names){
var finalNames = names.forEach(returnNames);
console.log(typeof finalNames);
function returnNames() {
for (var i = 0; i<names.length; i++) {
var nameValue = Object.keys(names[i]).map(key => names[i][key])
}
}
在 forEach
中,您将 returnNames
作为回调传递。 forEach
将遍历数组的每个元素。将每个元素传递给提供的回调并执行它。
如您所见,在回调中,您再次使用 for 循环遍历整个列表。如此有效,对于每个元素,您再次遍历整个列表,将复杂性增加到 O(n2)
第二个问题,正如其他答案所指出的,您没有从 forEach
回调中返回任何内容。因此,finalNames
的值变为 undefined
。所以,当你 运行 这个语句
finalNames.length / i == 1
length
属性无法读取
function returnNameListString(namelist){
if (namelist.length){
return
}else if(namelist.length===1){
return toString(namelist)
}else{
nameString = namelist[0]
for(var i=1; i<(namelist.length-1);i++){
nameString = nameString + ", " + namelist[i]
}
nameString = nameString + " & " + namelist[namelist.length-1]
return nameString
}
}
试试这个!将名称声明为列表 ["tom"、"dick"、"harry"]