Home > Blockchain >  access to a propery of json data in json server for sending data
access to a propery of json data in json server for sending data

Time:12-05

i have json server fake data in my react app and here is my data:

{"quotes": [
{
  "id": 1,
  "quote": "Javascript is my life",
  "author": "ali pasha",
  "comments": [],
},
{
  "id": 2,
  "quote": "Learning react is very fun",
  "author": "nasim rabiei",
  "comments": []
}],}

this app is a quotes showing app that each quote has comments. in my quote form users can send comment but i can not access to the comments propery to send data. how can i access to this specific propery and fill it with comments that recive from users?

CodePudding user response:

quotes[0].comments[0]

Since quotes is an array of object and comments is array as well so to access the comments you will have to use array indexing or you can loop through the comments since it is an array

CodePudding user response:

    var quotesDetail= {"quotes": [
    {
      "id": 1,
      "quote": "Javascript is my life",
      "author": "ali pasha",
      "comments": [1],
    },
    {
      "id": 2,
      "quote": "Learning react is very fun",
      "author": "nasim rabiei",
      "comments": []
    }],};
    
    var inner = quotesDetail.quotes.map(function(e) {
      return e.comments;
    });
    
    console.log(inner);

  • Related