[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.javascript

is there a function enumerating all element in an object?

Stone Zhong

8/22/2015 5:45:00 AM

Hi there,

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;
};

Thanks,
Stone
13 Answers

ram

8/22/2015 10:07:00 AM

0

Stone Zhong <stone.zhong@gmail.com> writes:
>Is there a member function in ES5 or ES6 doing something like below?

I am not aware of such a function.

>var forEachItemInObject = function(obj, callback) {
> var ret = [ ];
> for (var key in obj) {
> ret.push(callback(key, obj[key]));
> }
> return ret;
>};

The function is constantly pushing to ret here, that is, the
function is building an intermediate object. It might be
cheaper just to yield. For example,

function* map( f, value )
{ "use strict";
for( let key in value )yield f( key, value[ key ]); }

This can be used as in, for example:

( function()
{ "use strict";

function* map( f, value )
{ "use strict"; for( let key in value )yield f( key, value[ key ]); }

function output( string )
{ "use strict"; console.log( string + "\n" );
document.write( string + "\n" ); }

function main()
{ "use strict";
{ const value = Object.create( null ); value.alpha = "beta";
const f = function( key, value ){ output( key + ", " + value ); };
{ const iterator = map( f, value );
while( !iterator.next().done ); }}}

return main; }) () ();

ram

8/22/2015 3:51:00 PM

0

Stone Zhong <stone.zhong@gmail.com> writes:
>Is there a member function in ES5 or ES6 doing something like below?

For arrays, there is Array.prototype.map().

nathank887

10/29/2015 5:11:00 AM

0

On Saturday, August 22, 2015 at 12:44:51 AM UTC-5, Stone Zhong wrote:
> Hi there,
>
> 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;
> };
>
> Thanks,
> Stone

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);
});


See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Arr...

donmaximo01

10/29/2015 1:08:00 PM

0

FOR My Price and Pictures On the Pills / vals contact me at

zita3322@gmail.com

waiting

JR

10/29/2015 5:59:00 PM

0

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

JR

10/29/2015 6:20:00 PM

0

On 29-10-2015 15:59, Joao Rodrigues wrote:
> 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;
>>> };

>
> In ES2015 (ES6), the OP's code might be:
>
> Object.keys(obj).map(key => callback(key, obj[key]));
>
>

Or:

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

--
Joao Rodrigues

Evertjan.

10/29/2015 7:59:00 PM

0

Joao Rodrigues <groups_jr-1@yahoo.com.br> wrote on 29 Oct 2015 in
comp.lang.javascript:

>> Object.keys(obj).map(key => callback(key, obj[key]));
> Or:
>
> Array.from(Object.keys(obj), key => callback(key, obj[key]));

Could you please explain to us simple minded how this realy works?



--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)

Ben Bacarisse

10/29/2015 8:49:00 PM

0

"Evertjan." <exxjxw.hannivoort@inter.nl.net> writes:

> Joao Rodrigues <groups_jr-1@yahoo.com.br> wrote on 29 Oct 2015 in
> comp.lang.javascript:
>
>>> Object.keys(obj).map(key => callback(key, obj[key]));
>> Or:
>>
>> Array.from(Object.keys(obj), key => callback(key, obj[key]));
>
> Could you please explain to us simple minded how this realy works?

In ES6, the form

x => expression

is similar to

function (x) { return expression; }

There are a few differences ("this" is lexical in an arrow function and
there is no "arguments" object) but neither of those matter here I don't
think.

[You may have multiple parameters:

(a, b) => a + b

and you may have an explicit block rather than a single expression:

x => { console.log(`x = ${x}`); return x; }

which sneaks in another ES6 feature: template strings.]

--
Ben.

Evertjan.

10/29/2015 9:50:00 PM

0

Ben Bacarisse <ben.usenet@bsb.me.uk> wrote on 29 Oct 2015 in
comp.lang.javascript:

> "Evertjan." <exxjxw.hannivoort@inter.nl.net> writes:
>
>> Joao Rodrigues <groups_jr-1@yahoo.com.br> wrote on 29 Oct 2015 in
>> comp.lang.javascript:
>>
>>>> Object.keys(obj).map(key => callback(key, obj[key]));
>>> Or:
>>>
>>> Array.from(Object.keys(obj), key => callback(key, obj[key]));
>>
>> Could you please explain to us simple minded how this realy works?
>
> In ES6, the form
>
> x => expression
>
> is similar to
>
> function (x) { return expression; }
>
> There are a few differences ("this" is lexical in an arrow function and
> there is no "arguments" object) but neither of those matter here I don't
> think.
>
> [You may have multiple parameters:
>
> (a, b) => a + b
>
> and you may have an explicit block rather than a single expression:
>
> x => { console.log(`x = ${x}`); return x; }
>
> which sneaks in another ES6 feature: template strings.]

Thank you, now I know the name, I will start here:

<https://developer.mozil...
US/docs/Web/JavaScript/Reference/Functions/Arrow_functions#Browser_compatibi
lity>



--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)

JR

10/30/2015 6:05:00 PM

0

Evertjan wrote:
> Ben Bacarisse wrote:
>
>> "Evertjan." <exxjxw.hannivoort@inter.nl.net> writes:
>>
>>> Joao Rodrigues wrote:
>>>
>>>>> Object.keys(obj).map(key => callback(key, obj[key]));
>>>> Or:
>>>>
>>>> Array.from(Object.keys(obj), key => callback(key, obj[key]));
>>>
>>> Could you please explain to us simple minded how this realy works?
>>
>> In ES6, the form
>>
>> x => expression
>>
>> is similar to
>>
>> function (x) { return expression; }
>> [snip]

> Thank you, now I know the name, I will start here:
>
> <https://developer.mozil...
> US/docs/Web/JavaScript/Reference/Functions/Arrow_functions#Browser_compatibi
> lity>

I hope this article will help you get started using ES2015 (6th edition):
<http://www.smashingmagazine.com/2015/10/es6-whats-new-next-version-javas...

--
Joao Rodrigues