[英]Object value by key return undefined
我有像下面這樣的對象,每次我調用 object["key"] 時它都會由於某種原因返回 undefined
我嘗試了以下操作,但仍然未定義
object.keyName
object[keyName]
object["keyName"]
他們都返回未定義
let sam = {
"facebook": "https://facebook.com",
"instagram": "https://Instagram.com",
"placeName": "dazzlement",
"snapchat": "https://snapchat.com",
"twitter": "https://twitter.com",
}
假設我想要我嘗試過的 placeName 值
sam.placeName
sam[placeName]
sam["placeName"]
他們都返回未定義。
- - - - 更新 - - - - -
我正在使用帶有 Parse JS SDK 的 react native 這里是從 Parse 數據庫中獲取值並將其作為道具發送到一個組件的代碼,它會將結果顯示為卡片
this.state = {
items: null,
loaded: false
};
componentDidMount() {
this.queryValues()
}
queryValues = async id => {
let cy = this;
const GameScore = Parse.Object.extend('social');
const query = new Parse.Query(GameScore);
query.equalTo('A', 1);
query.equalTo('B', 2);
const results = await query.find().then(
results => {
cy.setState({
items: results
loaded: true,
})
},
error => {}
);
};
render() {
return (
{
this.state.loaded ?
<FlatList
data = {this.state.items}
renderItem = {
({item}) => <SocialCard social={item} /> } /
>
: <
Text> Loading </Text>
}
);
}
}
在組件中,這里是代碼
render() {
const {
social
} = this.props;
console.log(social)
return (
<Text>{this.props.}</Text>
);
}
日志顯示如下
Object {
"facebook": "https://facebook.com",
"instagram": "https://Instagram.com",
"placeName": "dazzlement",
"snapchat": "https://snapchat.com",
"twitter": "https://twitter.com",
}
這應該返回所有鏈接
let sam = {
facebook: 'facebook.com', // you don't need " " for facebook
instagram: 'instagram.com'
}
for (let i in sam){
console.log(sam[i])
}
sam.facebook // facebook.com
sam.instagram // instagram.com
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.