for (var prop in p) {if (!p.hasOwnProperty(prop)) {//The current property is not a direct property of pcontinue;}//Do your logic with the property here}
In Underscore.js you can find method _.each(), which iterates over a list of elements, yielding each in turn to a supplied function (pay attention to the order of arguments in iteratee function!):
Lo-Dash provides several methods for iterating over object properties. Basic _.forEach() (or it's alias _.each()) is useful for looping through both objects and arrays, however (!) objects with length property are treated like arrays, and to avoid this behavior it is suggested to use _.forIn() and _.forOwn() methods (these also have value argument coming first):
var obj = {a : 1};for(var key in obj){//obj.hasOwnProperty(key) is not needed.console.log(key);}//then check if anybody has messed the native object. Put this code at the end of the page.for(var key in Object){throw new Error("Please don't extend the native object");}
for (var key in object) {if (p.hasOwnProperty(key)) {var value = object[key];console.log(key); // This is the key;console.log(value); // This is the value;}}
//no prototype manipulationfunction getObjIterator(obj) {//create a dummy object instead of adding functionality to all objectsvar iterator = new Object();
//give it what the shim does but as its own local propertyiterator[Symbol.iterator] = function() {var keys = Object.keys(obj)[Symbol.iterator]();var output;
return {next:function() {if (!(output = keys.next()).done)output.value = [output.value, obj[output.value]];return output;}};};
return iterator;}
现在你可以把它作为一个普通的函数来调用,其他的都不会受到影响
var realMap = new Map(getObjIterator({well:'hello', there:'!'}))
const MyObject = {'a': 'Hello','b': 'it\'s','c': 'me','d': 'you','e': 'looking','f': 'for',[Symbol.iterator]: function*() {for (const i of Object.keys(this)) {yield [i, this[i]];}}};
for (const [k, v] of MyObject) {console.log(`Here is key ${k} and here is value ${v}`);}
const MyObject = {'a': 'Hello','b': 'it\'s','c': 'me','d': 'you','e': 'looking','f': 'for',};
for (const [k, v] of Object.entries(MyObject)) {console.log(`Here is key ${k} and here is value ${v}`);}
Object.defineProperty(Object.prototype, 'forEach', {value: function (func) {for (var key in this) {if (!this.hasOwnProperty(key)) {// skip loop if the property is from prototypecontinue;}var value = this[key];func(key, value);}},enumerable: false});
对于那些不喜欢“为…在”方法的人:
Object.defineProperty(Object.prototype, 'forEach', {value: function (func) {var arr = Object.keys(this);for (var i = 0; i < arr.length; i++) {var key = arr[i];func(key, this[key]);}},enumerable: false});
// Evil response in a variable. Here are all my vehicles.let evilResponse = {"car" :{"color" : "red","model" : "2013"},"motorcycle":{"color" : "red","model" : "2016"},"bicycle":{"color" : "red","model" : "2011"}}// Step 1. Get all the object keys.let evilResponseProps = Object.keys(evilResponse);// Step 2. Create an empty array.let goodResponse = [];// Step 3. Iterate throw all keys.for (prop of evilResponseProps) {goodResponse.push(evilResponseProps[prop]);}
var p =[{"username":"ordermanageadmin","user_id":"2","resource_id":"Magento_Sales::actions"},{"username":"ordermanageadmin_1","user_id":"3","resource_id":"Magento_Sales::actions"}]for(var value in p) {for (var key in value) {if (p.hasOwnProperty(key)) {console.log(key + " -> " + p[key]);}}}
// 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}`);
}
Object.keys(使用 for-of循环,但可以使用任何循环构造):
// 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)}`);
}
}
var p = {
"p1": "value1",
"p2": "value2",
"p3": "value3"
};
for (var i in Object.entries(p)){
var key = Object.entries(p)[i][0];
var value = Object.entries(p)[i][1];
console.log('key['+i+']='+key+' '+'value['+i+']='+value);
}
var p = {
"p1": "value1",
"p2": "value2",
"p3": "value3"
};
var myMap = new Map( Object.entries(p) );
for ( let [k, v] of myMap.entries() ) {
console.log( `${k}: ${v}` )
}
<h3>ECMAScript 2017</h3>
<p><b>Object.entries()</b> makes it simple to convert <b>Object to Map</b>:</p>