简体   繁体   中英

How to get an object from another object if I do not know key name

in firebase i want to extract data but it returns object inside another object Object { "-LJFXZDI-O-qR572deOs": Object { "city": "almaty", "dob": "1995-08-06", "gender": "male", "height": "190", "userId": "LS1pYNjiIjRpSNV1xfXqngKAKjz2", "username": "aaaa", "weight": "80", }, }

i want to get inner object "city": "almaty", "dob": "1995-08-06", "gender": "male", "height": "190", "userId": "LS1pYNjiIjRpSNV1xfXqngKAKjz2", "username": "aaaa", "weight": "80",

but i do know this key "-LJFXZDI-O-qR572deOs" what should i do?

Here is how you access the key without knowing the key, using Object.keys:

 var obj = { "-LJFXZDI-O-qR572deOs": { "city": "almaty", "dob": "1995-08-06", "gender": "male", "height": "190", "userId": "LS1pYNjiIjRpSNV1xfXqngKAKjz2", "username": "aaaa", "weight": "80", }, } keys = Object.keys(obj); // all keys of the outer object myKey = keys[0]; // the unknown key of the inner object innerObject = obj[myKey]; city = innerObject.city; console.log(city); 

You can do:

 const obj = { "-LJFXZDI-O-qR572deOs": { "city": "almaty", "dob": "1995-08-06", "gender": "male", "height": "190", "userId": "LS1pYNjiIjRpSNV1xfXqngKAKjz2", "username": "aaaa", "weight": "80", } }; const key = Object.keys(obj)[0]; const city = obj[key].city; console.log(key); console.log(city); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM