1

我是 Javascript 新手,我想使用 document.getElementById 来显示项目的值,在我的示例中,我想列出打印在 var= btsFrontEnd 中的名称,但我做错了。任何帮助都感激不尽。

谢谢你。 链接到我的小提琴

var btsFrontEnd = {
     "employee-1": {
         "name": "Name One", 
         "phone": "1234567890",
         "email": "blah@blah.com"
        },

    "employee-2": {
                  "name": "Name Two", 
                  "phone": "1234567890",
                  "email": "blah@blah." 
     }
};


var btsemployees = {
    employees:[
        {
     "name": "Name One", 
     "phone": "1234567890",
     "email": "blah@blah.com"
    },
     { 
     "name": "Name Two", 
      "phone": "1234567890",
     "email": "blah@blah.com"
},
     { 
     "name": "Name Three", 
      "phone": "1234567890",
     "email": "blah@blah.com"
},
     { 
     "name": "Name Four", 
      "phone": "1234567890",
     "email": "blah@blah.com"
},
     {
     "name": "Name Five", 
      "phone": "1234567890",
     "email": "blah@blah.com"
}
]
};


//First argument is our data structure (an object or an array
//Second argument is a callback (logic we apply to our data structure)
$.each(btsFrontEnd, function (key, value) { 
console.log(key); //Prints this object's keys
console.log(value); //Prints immediate values from this object
console.log(btsFrontEnd[key]); 
console.log(value.name); 
    document.getElementById("names").innerHTML.value;// This is what I am referring to, I would like it to appear in the p id="names"

});
4

4 回答 4

2

这是jsfiddle:http: //jsfiddle.net/Ss2kk/7/

// Put names into an array
var employeeNames = [];
$.each(btsFrontEnd, function (employeeid, employee) { //first is the key or index, second argument is the value
    // Check each element if it has name field
    if (employee.name !== undefined) {
        // Put its name into the array
        employeeNames.push(employee.name);
    }
});

// Join the array as comma seperated and put the content into `<p>` tag.
document.getElementById("names").innerHTML = employeeNames.join(",");
于 2013-07-01T19:19:50.807 回答
0
var names = document.getElementById( 'names' );
names.innerHTML = '';

$.each( btsFrontEnd, function( key, value ) { 
    names.innerHTML += value.name + '<br>';    
});
于 2013-07-01T19:19:14.970 回答
0

在循环内部,键和值参数表示第一次迭代中的以下值:

key
    "employee-1"

value 
    Object { name="Name One", phone="1234567890", email="blah@blah.com"}

由于value是一个对象,您可以像这样访问名称:value.name.

于 2013-07-01T19:24:04.773 回答
0

列出的所有答案都使用 jQuery,所以我认为添加纯 JavaScript 版本会很有用。其实是两个版本。

第一个版本使用Object.keys,相对于第二个版本中使用的hasOwnProperty,它比较新。这意味着第二个版本兼容更多的浏览器。

版本 1:

// Declare variables
var keys, i, names = [];

// Get all keys in the btsFrontEnd object in an array. This is employee-1 and employee-2 in the example.
keys = Object.keys(btsFrontEnd);
// Loop over all keys
for(i = 0; i < keys.length; ++i) {
    // Get the name of the employee via it's key and add it to the name array.
    names.push(btsFrontEnd(keys[i]).name);
}
//Make one long string from the names with a comma as seperator, then put the string in the dom element.
document.getElementById("names").innerHTML = names.join(",");

版本 2:

// Declare variables
var key, names = [];

//Loop over all object properties
for(key in btsFrontEnd) {
    // Check if the property is defined by you, and is not a standard Object property.
    if(btsFrontEnd.hasOwnProperty(key)) {
        names.push(btsFrontEnd[key].name);
    }
}
//Make one long string from the names with a comma as seperator, then put the string in the dom element.
document.getElementById("names").innerHTML = names.join(",");
于 2013-07-01T20:13:17.623 回答