Accessing Object inside Array

风流意气都作罢 提交于 2019-12-12 18:54:12

问题


I'm trying to access values inside Firebase array > object.

When I try to access values inside v-for, it works well. But I cannot do this: postDetail.author. It returns undefined. What's the solution?


回答1:


Since postDetail is an array of object to access properties inside its objects, you need do something like postDetail[Index].prop

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



回答2:


If you want get only author try it:

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

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

console.log(inner);



回答3:


// 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));
});


来源:https://stackoverflow.com/questions/50822205/accessing-object-inside-array

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!