-1

I have the following object:

var myObj={
    
        "name": "Chatik",
        "type": "public_supergroup",
        "id": 9947542893,
        "messages": [
         {
          "id": 1,
          "type": "service",
          "date": "2019-11-11T21:45:33",
          "actor": "Chatik",
          "actor_id": 9947542893,
          "action": "migrate_from_group",
          "title": "Chatik",
          "text": ""
         },
         {
          "id": 2,
          "type": "message",
          "date": "2019-11-11T21:51:22",
          "from": "Korney Chukovsky",
          "from_id": 4528246494,
          "text": "Чому никто не вкатывается?"
         },
         {
          "id": 3,
          "type": "message",
          "date": "2019-11-11T21:55:13",
          "from": "Korney Chukovsky",
          "from_id": 4528246494,
          "text": "Бля, я даже в своей собственной конфе один."
         },
         {
           "id": 7,
           "type": "message",
           "date": "2019-11-11T22:05:48",
           "from": "Андрей",
           "from_id": 4855779304,
           "text": "ты откуда?"
               }
        ]};

How do I create a script that would output all "text properties from each "elements" within the curly braces but only if the "from_id" property matches a certain value?

For example if my "from id" value is 4528246494, then "text" value should be outputted to the console, then proceed to check the next "element", if the value is not 4528246494, then skip, etc.

SSJ5Broli
  • 49
  • 7

1 Answers1

1

You just have to access message and grab the second element as normal property accessing

var myObj = {
  "name": "Chatik",
  "type": "public_supergroup",
  "id": 9947542893,
  "messages": [{
      "id": 1,
      "type": "service",
      "date": "2019-11-11T21:45:33",
      "actor": "Chatik",
      "actor_id": 9947542893,
      "action": "migrate_from_group",
      "title": "Chatik",
      "text": ""
    },
    {
      "id": 2,
      "type": "message",
      "date": "2019-11-11T21:51:22",
      "from": "Korney Chukovsky",
      "from_id": 4528246494,
      "text": "Чому никто не вкатывается?"
    },
    {
      "id": 3,
      "type": "message",
      "date": "2019-11-11T21:55:13",
      "from": "Korney Chukovsky",
      "from_id": 4528246494,
      "text": "Бля, я даже в своей собственной конфе один."
    }
  ]
};

console.log(myObj.messages[1].text)
hgb123
  • 13,869
  • 3
  • 20
  • 38