我只想从数组中的每个对象中打印名称,并尝试将每个名称变成href标记或可单击。 我尝试了这段代码
let response = [{
"mobile": 9860612345,
"_id": "5d1ef9bb88fb7f105d48405c",
"name": "Bishal Gurung",
"address": "Boharatar",
"email": "bishal@gmail.com",
"owner": "5d1c50ee91723b3ca4052686",
"__v": 0
},
{
"mobile": 9860333445,
"_id": "5d1ff09180977225b9853705",
"name": "Shiva Tandava",
"address": "Kailash",
"email": "shiva@gmail.com",
"owner": "5d1c50ee91723b3ca4052686",
"__v": 0
},
{
"mobile": 9860331234,
"_id": "5d1ff0b580977225b9853706",
"name": "VENOM SHRESTHA",
"address": "Nayabazar",
"email": "venom@gmail.com",
"owner": "5d1c50ee91723b3ca4052686",
"__v": 0
}
]
var parsed = ""
for (i = 0; i < response.length; i++) {
var myobj = response[i];
for (var property in myobj) {
parsed += property + ": " + myobj[property] + "<br/><br/>";
}
}
document.getElementById("contacts_details").innerHTML = parsed;
<div id="contacts_details"></div>
此代码显示了数组的所有内容,但我只需要每个对象的名称。
答案 0 :(得分:0)
不要在每个对象上for...in
,而只需添加response[i].name
。另外,如果您希望将可点击的名称链接到该人的电子邮件地址,请在a
标签上附加指向该电子邮件的href
属性:
let response = [{
"mobile": 9860659972,
"_id": "5d1ef9bb88fb7f105d48405c",
"name": "Bishal Gurung",
"address": "Boharatar",
"email": "bishal@gmail.com",
"owner": "5d1c50ee91723b3ca4052686",
"__v": 0
},
{
"mobile": 9860333445,
"_id": "5d1ff09180977225b9853705",
"name": "Shiva Tandava",
"address": "Kailash",
"email": "shiva@gmail.com",
"owner": "5d1c50ee91723b3ca4052686",
"__v": 0
},
{
"mobile": 9860331234,
"_id": "5d1ff0b580977225b9853706",
"name": "VENOM SHRESTHA",
"address": "Nayabazar",
"email": "venom@gmail.com",
"owner": "5d1c50ee91723b3ca4052686",
"__v": 0
}
]
var parsed = ""
for (i = 0; i < response.length; i++) {
parsed += `name: <a href="mailto:${response[i].email}">${response[i].name}</a><br/><br/>`;
}
document.getElementById("contacts_details").innerHTML = parsed;
<div id="contacts_details"></div>