我已经完成了许多API调用,然后访问了他们的孩子,但是由于某些原因,我无法理解为什么这次我变得未定义。
当我做类似
的操作时if (!this.props.newsLoading) {
console.log(this.props.news["Data"]) //Line: 35
console.log(this.props.news["Data"][0]) //Line: 36
}
console.log(this.props.news["Data"])
在控制台中显示如下
(50) [{…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}]
0: {id: "436511", guid: "https://www.coinspeaker.com/?p=58225", published_on: 1538583679, imageurl: "https://images.cryptocompare.com/news/coinspeaker/8kMa6p_wh01.jpeg", title: "Swell 2018: TransferGo Claims to Have Reduced Payments Cost by 90% Using Ripple Blockchain", …}
1: {id: "435701", guid: "https://bitcoinmagazine.com/articles/bitcoin-core-0170-released-heres-whats-new/#1538573301", published_on: 1538573301, imageurl: "https://images.cryptocompare.com/news/bitcoinmagazine/9GQ8h3mEd80.jpeg", title: "Bitcoin Core 0.17.0 Is Released: Here’s What’s New", …}
2: {id: "435621", guid: "https://www.cryptoglobe.com/latest/2018/10/shapesh…rhees-hits-back-at-deceptive-wall-street-journal/", published_on: 1538572500, imageurl: "https://images.cryptocompare.com/news/cryptoglobe/9m8933jy2gA.png", title: "ShapeShift, Voorhees Hits Back at “Deceptive” Wall Street Journal", …}
3: {id: "435609", guid: "https://cointelegraph.com/news/ibm-awarded-patent-for-secure-system-based-on-blockchain", published_on: 1538572200, imageurl: "https://images.cryptocompare.com/news/cointelegraph/d203500a88w.jpeg", title: "IBM Awarded Patent for Secure System Based on Blockchain", …}
4: {id: "435576", guid: "https://www.financemagnates.com/?p=242353", published_on: 1538571946, imageurl: "https://images.cryptocompare.com/news/financemagnates/8BxOkES8009.jpeg", title: "Israel Securities Authority Now Using Blockchain Technology", …}
但是当我执行类似console.log(this.props.news["Data"][0])
的操作时,它正在控制台中记录未定义的内容。
问题:我在这里可能做错了什么?
注意:我不确定为什么人们会对此表示反对,但是如果他们也能说出同样的理由以便我改善问题,我将不胜感激
答案 0 :(得分:-2)
尝试一下:
if (!this.props.newsLoading) {
console.log(this.props.news["Data"]) //Line: 35
console.log((this.props.news["Data"])[0]) //Line: 36
}
代码的问题是,当您编写arr["data"][0]
时,它意味着您具有二维数组数据,即arr[dim1][dim2]
,但是在您的情况下,您只有一个维数组。因此,要从一维数组中获取数据,您需要用()
包围变量,以将其视为一维数组。希望我足够清楚!如果您仍有任何疑问,请在下面发表评论。干杯!