How can I get the name of the key name of an object?
In the example I want key to equal a, b and c.
let demo = { "a": valA, "b": valB, "c": valC };
$.each(demo, function (index, val) {
let key = ??
});
Use Object#keys :
const demo = { "a": "valA", "b": "valB", "c": "valC" }; const keys = Object.keys(demo); console.log(keys);
Since you're using ES6 syntax, you have access to the for ... in
loop, which can loop through objects.
const demo = { "a": valA, "b": valB, "c": valC };
for (const key in demo) {
console.log(key);
}
// a
// b
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.