Iterate over object keys in node.js

后端 未结 5 1736
伪装坚强ぢ
伪装坚强ぢ 2020-11-30 23:12

Since Javascript 1.7 there is an Iterator object, which allows this:

var a={a:1,b:2,c:3};
var it=Iterator(a);

function iterate(){
    try {  
        consol         


        
5条回答
  •  栀梦
    栀梦 (楼主)
    2020-11-30 23:52

    Also remember that you can pass a second argument to the .forEach() function specifying the object to use as the this keyword.

    // myOjbect is the object you want to iterate.
    // Notice the second argument (secondArg) we passed to .forEach.
    Object.keys(myObject).forEach(function(element, key, _array) {
      // element is the name of the key.
      // key is just a numerical value for the array
      // _array is the array of all the keys
    
      // this keyword = secondArg
      this.foo;
      this.bar();
    }, secondArg);
    

提交回复
热议问题