我有一个JSON类型的数据源,它具有以下结构:
{
"list":
[
{
"id": "4167845External",
"personId": 4167845,
"profileId": 4268472,
"personType": "External",
"cat": "person",
"imageId": "10300600",
"extn": "88976",
"profileUrl":
[
"id-mark",
"mar123"
]}],
对于数组profileUrl
,我编写了以下JavaScript代码,以便在网页上显示:
function Staff(person) {
var list = "";
for (var i = 0; i < person.length; ++i) {
var record = person[i];
list +=
"<tr><td>"
+ record.profileUrl
+ "</td><tr>\n";
}
document.getElementById("showList").innerHTML = list;
}
,当我运行时,页面会显示id-mark,mar123
,但如果我只想显示mar123
该怎么办?
答案 0 :(得分:1)
profileUrl
是一个数组,因此您必须像访问数组一样访问该属性,例如record.profileUrl[1]
:
function Staff(person) {
var list = "";
for (var i = 0; i < person.length; ++i) {
var record = person[i];
list +=
"<tr><td>"
+ record.profileUrl[1]
+ "</td><tr>\n";
}
document.getElementById("showList").innerHTML = list;
}
JavaScript中的数组定义如下:
var myVar = [ "id-mark", "id-mark" ];
可以像这样访问每个元素:
console.log( myVar[0] ); // outputs: id-mark
console.log( myVar[1] ); // outputs: mar123
或者,可以像这样循环:
for (var i=0; i<myVar.length; i++) {
console.log(myVar[i]);
}