Accessing Object inside Array

15,795

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

Solution 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);

Share:
15,795
Flæx
Author by

Flæx

Updated on June 04, 2022

Comments

  • Flæx
    Flæx almost 2 years

    I'm trying to access values inside Firebase array > object. enter image description here

    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?

    • mplungjan
      mplungjan almost 6 years
      Please post CODE instead of PICTURES of code. You want postDetail[0].author
    • Narendra Jadhav
      Narendra Jadhav almost 6 years
      what you have tried so far ?
    • belicam
      belicam almost 6 years
      postDetail is an array and you want to access first item of it..so you need to do postDetail[0].author
    • mplungjan
      mplungjan almost 6 years
      This question was caused by a problem that can no longer be reproduced or a simple typographical error. While similar questions may be on-topic here, this one was resolved in a manner unlikely to help future readers. This can often be avoided by identifying and closely inspecting the shortest program necessary to reproduce the problem before posting.
  • Nicolas Pennec
    Nicolas Pennec almost 6 years
    and just for fun, with destructuring const [{author},] = [{"author" : "abc", "meta" : "xyz"}]; console.log(author); :-)