我用一些这样的对象填充了一个数组,并用JSON.stringifty(obj, null, '\t');打印出来

给我这样的输出:

[
    {
        "title": "here's the title"
    },
    {
        "description": "this is a description"
    }
]


现在,我正在尝试从具有内部对象的数组中获取数据。使用array.map像这样:

var title = objArray.map(function(a) {return a.title;});


当我做:

console.log(title); //the output looks like this
,here's the title,,,


如果我像这样手动进入数组

console.log(results[0]['title']); //the output is well formatted
here's the title


为什么会这样,又如何获取map函数不将那些额外的逗号添加到返回值中呢?

最佳答案

是的,因为您数组中的2个元素是:

{
    "title": "here's the title"
}




{
    "description": "this is a description"
}


但是它们没有相同的属性:
因此,当您尝试在第二个元素中显示属性title时,JS解释器仅返回undefined

07-24 09:44
查看更多