Mots clés : javascriptloopsfor-loopeachjavascript
100
var p = { "p1": "value1", "p2": "value2", "p3": "value3" }; for (var key in p) { if (p.hasOwnProperty(key)) { console.log(key + " -> " + p[key]); } }
var p = { 0: "value1", "b": "value2", key: "value3" }; for (var key of Object.keys(p)) { console.log(key + " -> " + p[key]) }
const p = { "p1": "value1", "p2": "value2", "p3": "value3" }; for (let [key, value] of Object.entries(p)) { console.log(`${key}: ${value}`); }
89
var obj = { first: "John", last: "Doe" }; Object.keys(obj).forEach(function(key) { console.log(key, obj[key]); });
for (const key of Object.keys(obj)) { console.log(key, obj[key]); }
Object.entries(obj).forEach( ([key, value]) => console.log(key, value) );
for (const [key, value] of Object.entries(obj)) { console.log(key, value); }
72
for (var prop in p) { if (!p.hasOwnProperty(prop)) { //The current property is not a direct property of p continue; } //Do your logic with the property here }
68
$.each(obj, function(key, value) { console.log(key, value); });
_.each(obj, function(value, key) { console.log(key, value); });
_.forIn(obj, function(value, key) { console.log(key, value); });
53
// A prototype object to inherit from, with a string-named property const p = {answer: 42}; // The object we'll look at, which inherits from `p` const o = Object.create(p); // A string-named property o.question = "Life, the Universe, and Everything"; // A symbol-named property o[Symbol("author")] = "Douglas Adams"; for (const name in o) { const value = o[name]; console.log(`${name} = ${value}`); }
// A prototype object to inherit from, with a string-named property const p = {answer: 42}; // The object we'll look at, which inherits from `p` const o = Object.create(p); // A string-named property o.question = "Life, the Universe, and Everything"; // A symbol-named property o[Symbol("author")] = "Douglas Adams"; for (const name of Object.keys(o)) { const value = o[name]; console.log(`${name} = ${value}`); }
// A prototype object to inherit from, with a string-named property const p = {answer: 42}; // The object we'll look at, which inherits from `p` const o = Object.create(p); // A string-named property o.question = "Life, the Universe, and Everything"; // A symbol-named property o[Symbol("author")] = "Douglas Adams"; for (const value of Object.values(o)) { console.log(`${value}`); }
// A prototype object to inherit from, with a string-named property const p = {answer: 42}; // The object we'll look at, which inherits from `p` const o = Object.create(p); // A string-named property o.question = "Life, the Universe, and Everything"; // A symbol-named property o[Symbol("author")] = "Douglas Adams"; for (const [name, value] of Object.entries(o)) { console.log(`${name} = ${value}`); }
// A prototype object to inherit from, with a string-named property const p = {answer: 42}; // The object we'll look at, which inherits from `p` const o = Object.create(p); // A string-named property o.question = "Life, the Universe, and Everything"; // A symbol-named property o[Symbol("author")] = "Douglas Adams"; for (const name of Object.getOwnPropertyNames(o)) { const value = o[name]; console.log(`${name} = ${value}`); }
// A prototype object to inherit from, with a string-named property const p = {answer: 42}; // The object we'll look at, which inherits from `p` const o = Object.create(p); // A string-named property o.question = "Life, the Universe, and Everything"; // A symbol-named property o[Symbol("author")] = "Douglas Adams"; for (const name of Object.getOwnPropertySymbols(o)) { const value = o[name]; console.log(`${String(name)} = ${value}`); }
// A prototype object to inherit from, with a string-named property const p = {answer: 42}; // The object we'll look at, which inherits from `p` const o = Object.create(p); // A string-named property o.question = "Life, the Universe, and Everything"; // A symbol-named property o[Symbol("author")] = "Douglas Adams"; for (const name of Reflect.ownKeys(o)) { const value = o[name]; console.log(`${String(name)} = ${value}`); }
// A prototype object to inherit from, with a string-named property const p = {answer: 42}; // The object we'll look at, which inherits from `p` const o = Object.create(p); // A string-named property o.question = "Life, the Universe, and Everything"; // A symbol-named property o[Symbol("author")] = "Douglas Adams"; for (let depth = 0, current = o; current; ++depth, current = Object.getPrototypeOf(current)) { for (const name of Reflect.ownKeys(current)) { const value = o[name]; console.log(`[${depth}] ${String(name)} = ${String(value)}`); } }
.as-console-wrapper { max-height: 100% !important; }