Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用javascript迭代JSON数组_Javascript_Json - Fatal编程技术网

使用javascript迭代JSON数组

使用javascript迭代JSON数组,javascript,json,Javascript,Json,我在使用javascript从json中提取两个数组并将它们输出到引导列表时遇到问题 通过测试,我已经能够做到这一点。我不太明白怎么了 var数据=[{ “标题”:“Ligo First Light”, “日期”:“2012-06-23”, “类别”:“已计划”, “维基百科”:“第一次直接观测引力波是在2015年9月14日进行的,并于2016年2月11日由LIGO和处女座合作组织宣布。[3][4][5]此前,引力波只是通过它们对双星系统中脉冲星时间的影响间接推断出来的。LIGO两个天文台检测

我在使用javascript从json中提取两个数组并将它们输出到引导列表时遇到问题

通过测试,我已经能够做到这一点。我不太明白怎么了

var数据=[{
“标题”:“Ligo First Light”,
“日期”:“2012-06-23”,
“类别”:“已计划”,
“维基百科”:“第一次直接观测引力波是在2015年9月14日进行的,并于2016年2月11日由LIGO和处女座合作组织宣布。[3][4][5]此前,引力波只是通过它们对双星系统中脉冲星时间的影响间接推断出来的。LIGO两个天文台检测到的波形[6]与广义相对论的预测相匹配[7][8][9]一个引力波由一对约36和29个太阳质量的黑洞的向内螺旋和合并以及随后单个黑洞的衰荡产生。[注2]该信号被命名为GW150914(来自引力波和观测日期2015-09-14)。[3][11]这也是对双星黑洞合并的首次观测,证明了双星质量黑洞系统的存在,以及这种合并可能在当前宇宙时代发生的事实。”,
“youtube”:[{
“发布日期”:“1976-03-04T04:19:34.259Z”,
“url”:”https://www.youtube.com/embed/B4XzLDM3Py8"
},
{
“发布日期”:“1976-03-04T04:19:34.259Z”,
“url”:”https://www.youtube.com/embed/CKynfOx3-ac"
}
],
“条款”:[{
“标题”:“从双星黑洞合并观测引力波”,
“发布日期”:“1976-03-04T04:19:34.259Z”,
“url”:”https://physics.aps.org/featured-article-pdf/10.1103/PhysRevLett.116.061102"
},
{
“标题”:“首次观测引力波”,
“发布日期”:“1997-11-03T10:03:39.123Z”,
“url”:”https://en.wikipedia.org/wiki/First_observation_of_gravitational_waves"
},
{
“标题”:“爱因斯坦预言100年后发现的引力波”,
“发布日期”:“1997-11-03T10:03:39.123Z”,
“url”:”https://www.ligo.caltech.edu/news/ligo20160211"
}
]
}];
函数getArrayByName(名称){
返回数据过滤器(
功能(数据){
return data.name==name
}
);
}
var found=getArrayByName('youtube');
document.getElementById('output')。innerHTML=找到[0]。url

我想您正在寻找这样的产品:

var data = [{
  "title": "Ligo First Light",
  "date": "2012-06-23",
  "category": "scheduled",
  "wikipedia": "The first direct observation of gravitational waves was made on 14 September 2015 and was announced by the LIGO and Virgo collaborations on 11 February 2016.[3][4][5] Previously, gravitational waves had only been inferred indirectly, via their effect on the timing of pulsars in binary star systems. The waveform, detected by both LIGO observatories,[6] matched the predictions of general relativity[7][8][9] for a gravitational wave emanating from the inward spiral and merger of a pair of black holes of around 36 and 29 solar masses and the subsequent ringdown of the single resulting black hole.[note 2] The signal was named GW150914 (from Gravitational Wave and the date of observation 2015-09-14).[3][11] It was also the first observation of a binary black hole merger, demonstrating both the existence of binary stellar-mass black hole systems and the fact that such mergers could occur within the current age of the universe.",
  "youtube": [{
      "publishDate": "1976-03-04T04:19:34.259Z",
      "url": "https://www.youtube.com/embed/B4XzLDM3Py8"
    },
    {
      "publishDate": "1976-03-04T04:19:34.259Z",
      "url": "https://www.youtube.com/embed/CKynfOx3-ac"
    }
  ],
  "articles": [{
      "title": "Observation of Gravitational Waves from a Binary Black Hole Merger",
      "publishDate": "1976-03-04T04:19:34.259Z",
      "url": "https://physics.aps.org/featured-article-pdf/10.1103/PhysRevLett.116.061102"
    },
    {
      "title": "First observation of gravitational waves",
      "publishDate": "1997-11-03T10:03:39.123Z",
      "url": "https://en.wikipedia.org/wiki/First_observation_of_gravitational_waves"
    },
    {
      "title": "Gravitational Waves Detected 100 Years After Einstein's Prediction",
      "publishDate": "1997-11-03T10:03:39.123Z",
      "url": "https://www.ligo.caltech.edu/news/ligo20160211"
    }
  ]
}];


function getArrayByName(name) {
  return data.filter(
    function(item) {
      return item[name];
    }
  )[0][name];
}

var found = getArrayByName('youtube');

document.getElementById('output').innerHTML = found[0].url;

这里,我们首先按照name属性过滤数据数组。因此,将返回数据数组中包含所提供名称的所有元素。然后我们访问返回列表的第一个元素并返回name属性的值。

youtube“
name
没有任何内容?数组中也没有属性为
name
的对象,请添加预期的输出