foreach 带有函数的语句
foreach Statements with a function
好的,我正在尝试回答问题并学习编码。关于数组和使用 foreach 语句,函数必须保留,因为这个“函数 animalNames”必须保留在某个地方,但显然,我做错了什么,因为我将它返回为未定义。即使它产生了正确的数组,有人可以看看它并让我知道我做错了什么。
附件是代码和数组的图片以及我回答的问题。这就是我编写函数的方式。
const displayNames = [];
zooAnimals.forEach(function animalNames(element){
var display = "name: " + element.animal_name + ", " + "scientific: " + element.scientific_name
displayNames.push(display);
})
console.log(displayNames);
我再次得到正确的数组并且数据看起来正确...但是 animalNames 返回为未定义...我无法删除这部分我要保留它但我不知道如何处理它.
试试这个,它将函数 animalNames 定义为单独的函数
const displayNames = [];
const zooAnimals = [
{animal_name:"Jackel",scientific_name:"Canine"}
]
function animalNames({animal_name, scientific_name}){
return `name: ${animal_name}, scientific: ${scientific_name}`
}
zooAnimals.forEach((element)=>{
displayNames.push(animalNames(element));
})
console.log(displayNames);
我相信您正在关注 this challenge。在这种情况下,请确保正确阅读说明:
The zoos want to display both the scientific name and the animal name in front of the habitats.
Use animalNames to populate and return the displayNames array with only the animal name and scientific name of each animal.
displayNames will be an array of strings, and each string should follow this pattern: "name: {name}, scientific: {scientific name}"
因此,根据说明,您应该创建一个函数 animalNames()
,该函数会创建一个名为 displayNames
的数组,并且 returns 从您的函数 animalNames()
中创建该数组。数组 displayNames
包含结构 name: animal_name, scientific: scientific_name
.
的字符串
// you could set zooAnimals as a required parameter in your function
// but as it is defined in the same file you can access it directly as well
function animalNames() {
const displayNames = [];
// you have to do the forEach on zooAnimals here
zooAnimals.forEach(animal => displayNames.push(`name: ${animal.animal_name}, scientific: ${animal.scientific_name}`))
// return an array
return displayNames;
}
按照您的方式,函数 animalsName()
是一个匿名函数,仅存在于 zooAnimals.forEach()
调用中。因此,它是未定义的。
好的,我正在尝试回答问题并学习编码。关于数组和使用 foreach 语句,函数必须保留,因为这个“函数 animalNames”必须保留在某个地方,但显然,我做错了什么,因为我将它返回为未定义。即使它产生了正确的数组,有人可以看看它并让我知道我做错了什么。
附件是代码和数组的图片以及我回答的问题。这就是我编写函数的方式。
const displayNames = [];
zooAnimals.forEach(function animalNames(element){
var display = "name: " + element.animal_name + ", " + "scientific: " + element.scientific_name
displayNames.push(display);
})
console.log(displayNames);
我再次得到正确的数组并且数据看起来正确...但是 animalNames 返回为未定义...我无法删除这部分我要保留它但我不知道如何处理它.
试试这个,它将函数 animalNames 定义为单独的函数
const displayNames = [];
const zooAnimals = [
{animal_name:"Jackel",scientific_name:"Canine"}
]
function animalNames({animal_name, scientific_name}){
return `name: ${animal_name}, scientific: ${scientific_name}`
}
zooAnimals.forEach((element)=>{
displayNames.push(animalNames(element));
})
console.log(displayNames);
我相信您正在关注 this challenge。在这种情况下,请确保正确阅读说明:
The zoos want to display both the scientific name and the animal name in front of the habitats. Use animalNames to populate and return the displayNames array with only the animal name and scientific name of each animal. displayNames will be an array of strings, and each string should follow this pattern: "name: {name}, scientific: {scientific name}"
因此,根据说明,您应该创建一个函数 animalNames()
,该函数会创建一个名为 displayNames
的数组,并且 returns 从您的函数 animalNames()
中创建该数组。数组 displayNames
包含结构 name: animal_name, scientific: scientific_name
.
// you could set zooAnimals as a required parameter in your function
// but as it is defined in the same file you can access it directly as well
function animalNames() {
const displayNames = [];
// you have to do the forEach on zooAnimals here
zooAnimals.forEach(animal => displayNames.push(`name: ${animal.animal_name}, scientific: ${animal.scientific_name}`))
// return an array
return displayNames;
}
按照您的方式,函数 animalsName()
是一个匿名函数,仅存在于 zooAnimals.forEach()
调用中。因此,它是未定义的。