gpt4 book ai didi

像对象 .map() 这样的 JavaScript 数组

转载 作者:搜寻专家 更新时间:2023-11-01 04:21:45 25 4
gpt4 key购买 nike

您好,我想使用 .map 来切换数组的排名和麦片,但是我在 console.log 中收到未定义的信息。基于令人敬畏的反馈,我能够让一切正常工作,但我仍然对某些事情感到困惑。因为我不确定如何将 Cereal 与排名倒序匹配?我完全被难住了。

var breakFastFood =[
{
cereal: "Captain Crunch",
scale: "Yuck!"

},
{
cereal: "Grape Nuts",
scale: "Yum!"

},
{
cereal: "Fruity Pebbles",
scale: "Yuck!"

},
{
cereal: "Oatmeal",
scale: "Yum!"

}
];
var cereals = breakFastFood.map(function(bFood){
return breakFastFood.cereal
});

var rank = breakFastFood.map(function(standing){
return breakFastFood.scale
});

rank.forEach(function(rating){console.log(rating)});
cereals.forEach(function(food){console.log(food)});

最佳答案

您没有在return 语句中使用函数参数:

var breakFastFood =[
{
cereal: "Captain Crunch",
scale: "Yuck!"

},
{
cereal: "Grape Nuts",
scale: "Yum!"

},
{
cereal: "Fruity Pebbles",
scale: "Yuck!"

},
{
cereal: "Oatmeal",
scale: "Yum!"

}
];
var cereals = breakFastFood.map(function(bFood){
return bFood.cereal
});

var rank = breakFastFood.map(function(standing){
return standing.scale
});

rank.forEach(function(rating){console.log(rating)});
cereals.forEach(function(food){console.log(food)});

你也可以使用简写属性:

var breakFastFood =[
{
cereal: "Captain Crunch",
scale: "Yuck!"

},
{
cereal: "Grape Nuts",
scale: "Yum!"

},
{
cereal: "Fruity Pebbles",
scale: "Yuck!"

},
{
cereal: "Oatmeal",
scale: "Yum!"

}
];
var cereals = breakFastFood.map(({cereal}) => cereal);

var rank = breakFastFood.map(({scale}) => scale);

rank.forEach(function(rating){console.log(rating)});
cereals.forEach(function(food){console.log(food)});

关于像对象 .map() 这样的 JavaScript 数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55036352/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com