我需要获得价值" 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的值中,并使用:
迭代epgfor (var k = 0; k < epg.length; k++) {
console.log(_epg[k]);
}
我需要将对象的名称与另一个变量(channelId)进行比较:
for (var k = 0; k < _epg.length; k++) {
if (channelId == epg[k]) {
console.log(_epg[k]);
}
}
答案 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
来说明
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;
答案 3 :(得分:0)
您可以使用Object.entries方法获取这些值。
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/entries
答案 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';
}
}
这将产生以下输出: