访问Array中的Object

时间:2018-06-12 16:58:37

标签: javascript vue.js nuxt.js

我尝试访问Firebase数组中的值>宾语。 enter image description here

当我尝试访问v-for中的值时,它运行良好。但我不能这样做:postDetail.author。它返回undefined。解决方案是什么?

3 个答案:

答案 0 :(得分:1)

由于postDetail是一个访问其对象内部属性的对象数组,因此您需要执行postDetail[Index].prop

之类的操作。

var postDetail =[{"author" : "abc", "meta" : "xyz"}];
console.log(postDetail[0].author);

答案 1 :(得分:1)

如果您只想获得author,请尝试:

var postDetails = [{
  author: "John",
  category: "Tech"
}];

var inner = postDetails.map(function(e) {
  return e.autor;
});

console.log(inner);

答案 2 :(得分:0)

// Array of object
var persons = [
  {
    name: "shubham",
    age: 22,
    comments: ["Good", "Awesome"]
  },
  {
    name: "Ankit",
    age: 24,
    comments: ["Fine", "Decent"]
  },
  {
    name: "Arvind",
    age: 26,
    comments: ["Awesome", "Handsome"]
  },
  {
    name: "Ashwani",
    age: 28,
    comments: ["Very Good", "Lovely"]
  }
];

var data = persons.map(person => {
  console.log(person.name);
  console.log(person.age);
  person.comments.map((comment, index) => console.log(index + " " + comment));
});