摆脱[object Object] javascript

时间:2015-09-02 06:41:43

标签: javascript arrays json object for-loop

我正在循环一个数组,以便检索其中的对象。

vm.dataFilesList = [{
    title: 'madrid',
    name: 'castles'
}, {
    title: 'spain',
    name: 'horses'
}, {
    title: 'story',
    name: 'oslo'
}, {
    title: 'beach',
    name: 'miami'
}];


for (var j = 0; j < vm.dataFilesList.length; j++) {
    console.log('file ' + vm.dataFilesList[j])
}

我在控制台中看到的是:[object Object]四次;而不是:

file  Object {title: "madrid", name: "castles"}
file  Object {title: "spain", name: "horses"}
file  Object {title: "story", name: "oslo"} 
file  Object {title: "beach", name: "miami"}

有什么建议吗?

谢谢!

2 个答案:

答案 0 :(得分:0)

你应该尝试:

console.log('file ' + JSON.stringify(vm.dataFilesList[j]))

答案 1 :(得分:0)

试试这个

for (var j = 0; j < vm.dataFilesList.length; j++) {
    console.log('file ' + vm.dataFilesList[j].title + ' ' + vm.dataFilesList[j].name);
}