如何获取JSON对象' JSON数组中的名称

时间:2018-03-29 12:09:27

标签: javascript jquery arrays json

我需要获得价值" channelA"和" channelB",它们是对象名称。

JSON文件:

[
    {
        "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,                     
               }
           ]
       }
   }
]    

我将JSON存储在名为epg的值中,并使用:

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

enter image description here

我需要将对象的名称与另一个变量(channelId)进行比较:

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

6 个答案:

答案 0 :(得分:1)

如果您只是想要这些名称,您可以简单地map覆盖数组并返回Object.keys数组中的第一个元素:

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

答案 1 :(得分:1)

使用Object.keys()方法:

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

答案 2 :(得分:1)

由于键是唯一的,您可以使用函数filter找到对象,如下所示:

我已添加两个channeldB来说明

&#13;
&#13;
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);
&#13;
.as-console-wrapper { max-height: 100% !important; top: 0; }
&#13;
&#13;
&#13;

答案 3 :(得分:0)

答案 4 :(得分:0)

您可以检查数组中每个对象内部的键,并与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]);         
    }   
}

答案 5 :(得分:0)

你可能想要遍历json:

// a function to hide all divs
const hideDivs = function (divs : HTMLCollection) {
    for (var div of divs) {
        (div as HTMLElement).style.display = 'none';
    }
}

这将产生以下输出:

  • “id 10”
  • “class child-of-9”
  • “id 11”
  • “10岁的孩子”

这可能会有所帮助:https://stackoverflow.com/a/1112609/3504189