0

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

6 Answers 6

1

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

Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

1

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"
]

Comments

0

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

Comments

0

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

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.