burakk
burakk

Reputation: 1291

How to Get the JSON Objects' Name in an JSON Array

I need to get the values "channelA" and "channelB", which are the object names.

JSON file:

[
    {
        "channelA": {
            "programmes": [
                {
                    "start_utc": 1522208700,
                    "stop_utc": 1522209600,
                },
                {
                    "start_utc": 1522209600,
                    "stop_utc": 1522210800,
                }
            ]    
        }
   },
   {
      "channelB": {
          "programmes": [
               {
                  "start_utc": 1522256700,
                  "stop_utc": 1522260800                     
               },
               {
                  "start_utc": 152229000,
                  "stop_utc": 1522318900,                     
               }
           ]
       }
   }
]    

I am storing the JSON in a value called epg, and iterating through epg with:

for (var k = 0; k < epg.length; k++) {          
    console.log(_epg[k]);   
}

enter image description here

I need to compare the name of the object with another variable (channelId):

for (var k = 0; k < _epg.length; k++) {
    if (channelId == epg[k]) {
       console.log(_epg[k]);
    }
}

Upvotes: 0

Views: 76

Answers (6)

Andy
Andy

Reputation: 63587

If you just want those names you can simply map over the array and return the first element from the Object.keys array:

const data = [{"channelA":{"programmes":[{"start_utc":1522208700,"stop_utc":1522209600,},{"start_utc":1522209600,"stop_utc":1522210800,}]}},{"channelB":{"programmes":[{"start_utc":1522256700,"stop_utc":1522260800},{"start_utc":152229000,"stop_utc":1522318900,}]}}]

const out = data.map(obj => Object.keys(obj)[0]);
console.log(out);

RESULT

[
  "channelA",
  "channelB"
]

Upvotes: 1

Ele
Ele

Reputation: 33736

Since keys are uniques, you can find the object using the function filter as follow:

I've added two channeldB to illustrate

var array = [    {        "channelA": {            "programmes": [                {                    "start_utc": 1522208700,                    "stop_utc": 1522209600,                },                {                    "start_utc": 1522209600,                    "stop_utc": 1522210800,                }            ]            }   },   {      "channelB": {          "programmes": [               {                  "start_utc": 1522256700,                  "stop_utc": 1522260800                                    },               {                  "start_utc": 152229000,                  "stop_utc": 1522318900,                                    }           ]       }   },   {      "channelB": {          "programmes": [               {                  "start_utc": 15232256700,                  "stop_utc": 1599990800                                    },               {                  "start_utc": 992229000,                  "stop_utc": 1520910900,                                    }           ]       }   }];

var channelId = "channelB";
var found = array.filter((a) => a[channelId]);
console.log(found);
.as-console-wrapper { max-height: 100% !important; top: 0; }

Upvotes: 1

ThoPaz
ThoPaz

Reputation: 174

You might want to iterate over the json:

var arr = [ {"id":"10", "class": "child-of-9"}, {"id":"11", "classd": "child-of-10"}];

for (var i = 0; i < arr.length; i++){
    var obj = arr[i];
    for (var key in obj){
        var attrName = key;
        var attrValue = obj[key];
        console.log(attrName + " " + attrValue)
    }
}

This will produce the following output:

  • "id 10"
  • "class child-of-9"
  • "id 11"
  • "classd child-of-10"

This might help: https://stackoverflow.com/a/1112609/3504189

Upvotes: 0

Ankit Agarwal
Ankit Agarwal

Reputation: 30729

You can check the key inside each object of the array and compare with the channelId:

var _epg = [
    {
        "channelA": {
            "programmes": [
                {
                    "start_utc": 1522208700,
                    "stop_utc": 1522209600,
                },
                {
                    "start_utc": 1522209600,
                    "stop_utc": 1522210800,
                }
            ]    
        }
   },
   {
      "channelB": {
          "programmes": [
              {
                  "start_utc": 1522256700,
                  "stop_utc": 1522260800                     
              },
              {
                  "start_utc": 152229000,
                  "stop_utc": 1522318900,                     
               }
           ]
       }
   }
];

var channelId = 'channelB';
for(var k=0; k<_epg.length; k++){
    var key = Object.keys(_epg[k])[0];
    if (channelId == key){
       console.log(_epg[k]);         
    }   
}

Upvotes: 0

messerbill
messerbill

Reputation: 5639

Use Object.keys() method for this:

let myArr = [
    {
        "channelA": {
            "programmes": [
                {
                    "start_utc": 1522208700,
                    "stop_utc": 1522209600,
                },
                {
                    "start_utc": 1522209600,
                    "stop_utc": 1522210800,
                }
            ]    
        }
   },
   {
      "channelB": {
          "programmes": [
              {
                  "start_utc": 1522256700,
                  "stop_utc": 1522260800                     
              },
              {
                  "start_utc": 152229000,
                  "stop_utc": 1522318900,                     
               }
           ]
       }
   }
]    

for (let item of myArr) {
  for (let key of Object.keys(item)) {
    if (key == "channelA" || key == "channelB") console.log(key + ": ", item[key])
  }
}

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/keys

Upvotes: 1

Stefan Maretić
Stefan Maretić

Reputation: 51

You can use the Object.entries method to get those values.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/entries

Upvotes: 0

Related Questions