我只是在学习JS,并且正在使用map()方法进行一些练习,因此很难。我正在处理数组中的对象,我想使用map()只返回属性的值。我可以将整个对象作为数组返回,也可以在单个索引处返回属性值,但似乎无法仅返回数组中的属性值。知道这可能只是一个简单的语法问题,但我无法弄清楚。这是我的代码:
function getStudentNames(students){
var stuObj = [students];
var justName = stuObj.map((x) => {
return x; // this will return an array of the objects or return x[0].name
//will return the name property value, but I can't seem to
//return just the name value as an array. I would think that
//return x.name; would work.
});
return justName;
}
getStudentNames([{{name:'Steve',},{name:'Mike'},{name:'John'}]))