Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #28860

Re: is there a function enumerating all element in an object?

From Joao Rodrigues <groups_jr-1@yahoo.com.br>
Newsgroups comp.lang.javascript
Subject Re: is there a function enumerating all element in an object?
Date 2015-10-29 15:59 -0200
Organization Aioe.org NNTP Server
Message-ID <n0tmpr$ia$1@speranza.aioe.org> (permalink)
References <7cb49130-72c4-48ee-b968-60a5e7648855@googlegroups.com> <f26e3952-78f4-49cd-ad94-2a451530fd17@googlegroups.com>

Show all headers | View raw


nathank887@gmail.com wrote:
> Stone Zhong wrote:
>> Is there a member function in ES5 or ES6 doing something like below? I saw a repeated pattern of such code, especially if you use reactjs
>>
>> var forEachItemInObject = function(obj, callback) {
>>   var ret = [ ];
>>   for (var key in obj) {
>>     ret.push(callback(key, obj[key]));
>>   }
>>   return ret;
>> };
>>

> 
> Use Object.keys(objectName) to get an array of keys, then you can use forEach(). For example, this alerts each key in myObject:
> 
> 
> Object.keys(myObject).forEach(function(key){
>     alert(key);
> });
> 

In ES2015 (ES6), the OP's code might be:

Object.keys(obj).map(key => callback(key, obj[key]));


-- 
Joao Rodrigues

Back to comp.lang.javascript | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

is there a function enumerating all element in an object? Stone Zhong <stone.zhong@gmail.com> - 2015-08-21 22:44 -0700
  Re: is there a function enumerating all element in an object? nathank887@gmail.com - 2015-10-28 22:11 -0700
    Re: is there a function enumerating all element in an object? Joao Rodrigues <groups_jr-1@yahoo.com.br> - 2015-10-29 15:59 -0200
      Re: is there a function enumerating all element in an object? Joao Rodrigues <groups_jr-1@yahoo.com.br> - 2015-10-29 16:20 -0200
        Re: is there a function enumerating all element in an object? "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2015-10-29 20:58 +0100
          Re: is there a function enumerating all element in an object? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-10-29 20:49 +0000
            Re: is there a function enumerating all element in an object? "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2015-10-29 22:49 +0100
              Re: is there a function enumerating all element in an object? Joao Rodrigues <groups_jr-1@yahoo.com.br> - 2015-10-30 16:05 -0200
              Re: is there a function enumerating all element in an object? RobG <rgqld@iinet.net.au> - 2015-11-14 13:16 -0800
                Re: is there a function enumerating all element in an object? Chuck Anderson <seemysig@thisis.invalid> - 2015-11-14 17:58 -0700
                Re: is there a function enumerating all element in an object? Joao Rodrigues <groups_jr-1@yahoo.com.br> - 2015-11-16 14:26 -0200
  Re: is there a function enumerating all element in an object? donmaximo01@gmail.com - 2015-10-29 06:08 -0700

csiph-web