请帮我解决下面的任务。
我想显示一个对象值,该属性等于变量txt。如果txt = "my location"
,我想显示“我的位置”的值。
而且我想以这种方式显示它:
city:value1,
街道:value2var obj = {“my name”: “first second”, “my location”: {“city”: “value1”, “street”: “value2”}};
objkeys = Object.keys(obj);
for (var i = 0; i < objkeys.length; ++i) console.log(objkeys[i]);
var txt = “my location”;
if (txt === objkeys[i]) console.log(???)
应该写什么?
谢谢!
答案 0 :(得分:0)
var obj = {"my name": "first second", "my location": {"city": "value1", "street": "value2"}};
var txt = "my location"
console.log(obj[txt])
// {"city": "value1", "street": "value2"}
答案 1 :(得分:0)
var obj = {"my name": "first second", "my location": {"city": "value1", "street": "value2"}};
var text="my location"
for (var key in obj[text]){
console.log(key+':'+obj[text][key])
}
使用obj [text]获取&#34;我的位置&#34;的值在obj中,你会得到一个物体。然后,对于此obj的每个键,您可以获得&#39;键&#39;直接并使用该键获取每个键的值,在我的位置&#34;通过obj [text] [key]