Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.javascript > #28553 > unrolled thread
| Started by | Stone Zhong <stone.zhong@gmail.com> |
|---|---|
| First post | 2015-08-21 22:44 -0700 |
| Last post | 2015-10-29 06:08 -0700 |
| Articles | 12 — 8 participants |
Back to article view | Back to comp.lang.javascript
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
| From | Stone Zhong <stone.zhong@gmail.com> |
|---|---|
| Date | 2015-08-21 22:44 -0700 |
| Subject | is there a function enumerating all element in an object? |
| Message-ID | <7cb49130-72c4-48ee-b968-60a5e7648855@googlegroups.com> |
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
[toc] | [next] | [standalone]
| From | nathank887@gmail.com |
|---|---|
| Date | 2015-10-28 22:11 -0700 |
| Message-ID | <f26e3952-78f4-49cd-ad94-2a451530fd17@googlegroups.com> |
| In reply to | #28553 |
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/Array/forEach
[toc] | [prev] | [next] | [standalone]
| From | Joao Rodrigues <groups_jr-1@yahoo.com.br> |
|---|---|
| Date | 2015-10-29 15:59 -0200 |
| Message-ID | <n0tmpr$ia$1@speranza.aioe.org> |
| In reply to | #28858 |
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
[toc] | [prev] | [next] | [standalone]
| From | Joao Rodrigues <groups_jr-1@yahoo.com.br> |
|---|---|
| Date | 2015-10-29 16:20 -0200 |
| Message-ID | <n0to0e$3im$1@speranza.aioe.org> |
| In reply to | #28860 |
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
[toc] | [prev] | [next] | [standalone]
| From | "Evertjan." <exxjxw.hannivoort@inter.nl.net> |
|---|---|
| Date | 2015-10-29 20:58 +0100 |
| Message-ID | <XnsA542D564FBADCeejj99@194.109.6.166> |
| In reply to | #28861 |
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)
[toc] | [prev] | [next] | [standalone]
| From | Ben Bacarisse <ben.usenet@bsb.me.uk> |
|---|---|
| Date | 2015-10-29 20:49 +0000 |
| Message-ID | <87ziz1flii.fsf@bsb.me.uk> |
| In reply to | #28862 |
"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.
[toc] | [prev] | [next] | [standalone]
| From | "Evertjan." <exxjxw.hannivoort@inter.nl.net> |
|---|---|
| Date | 2015-10-29 22:49 +0100 |
| Message-ID | <XnsA542E83B4EE2eejj99@194.109.6.166> |
| In reply to | #28863 |
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.mozilla.org/en-
US/docs/Web/JavaScript/Reference/Functions/Arrow_functions#Browser_compatibi
lity>
--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
[toc] | [prev] | [next] | [standalone]
| From | Joao Rodrigues <groups_jr-1@yahoo.com.br> |
|---|---|
| Date | 2015-10-30 16:05 -0200 |
| Message-ID | <n10bgq$fgi$1@speranza.aioe.org> |
| In reply to | #28864 |
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.mozilla.org/en-
> 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-javascript/>
--
Joao Rodrigues
[toc] | [prev] | [next] | [standalone]
| From | RobG <rgqld@iinet.net.au> |
|---|---|
| Date | 2015-11-14 13:16 -0800 |
| Message-ID | <8c0e17f6-9c66-4cc2-a157-e444c68fe975@googlegroups.com> |
| In reply to | #28864 |
On Friday, October 30, 2015 at 7:49:49 AM UTC+10, Evertjan. wrote: > 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? [...] > > Thank you, now I know the name, I will start here: > > <https://developer.mozilla.org/en- > US/docs/Web/JavaScript/Reference/Functions/Arrow_functions#Browser_compatibi > lity> There is always the ECMAScript 2015 specification[1], but it's impossibly turgid in many places. MDN is becoming an increasingly important resource. 1. <http://ecma-international.org/ecma-262/6.0/index.html#sec-arrow-function-definitions> -- Rob
[toc] | [prev] | [next] | [standalone]
| From | Chuck Anderson <seemysig@thisis.invalid> |
|---|---|
| Date | 2015-11-14 17:58 -0700 |
| Message-ID | <n28l80$d52$1@dont-email.me> |
| In reply to | #28887 |
RobG wrote: > On Friday, October 30, 2015 at 7:49:49 AM UTC+10, Evertjan. wrote: >> 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? > [...] >> Thank you, now I know the name, I will start here: >> >> <https://developer.mozilla.org/en- >> US/docs/Web/JavaScript/Reference/Functions/Arrow_functions#Browser_compatibi >> lity> > > There is always the ECMAScript 2015 specification[1], but it's impossibly turgid in many places. MDN is becoming an increasingly important resource. > > 1. <http://ecma-international.org/ecma-262/6.0/index.html#sec-arrow-function-definitions> > Probably steppin' in a pile of poo here (long time lurker and c.lang.php participant), but I only recently learned of Object.toSource, which displays the source code of an object - in Firefox. The Mozilla developers site warns that it should not be used on an active site as there are many incompatibilities (like no one else supports it?), but I have used it in the manner they suggest. It is very handy while debugging - within Firefox. -- ***************************** Chuck Anderson • Boulder, CO http://cycletourist.com Turn Off, Tune Out, Drop In *****************************
[toc] | [prev] | [next] | [standalone]
| From | Joao Rodrigues <groups_jr-1@yahoo.com.br> |
|---|---|
| Date | 2015-11-16 14:26 -0200 |
| Message-ID | <n2d03m$8eo$1@speranza.aioe.org> |
| In reply to | #28887 |
Em 14/11/2015 19:16, RobG escreveu: > On Friday, October 30, 2015 at 7:49:49 AM UTC+10, Evertjan. wrote: >> >> Thank you, now I know the name, I will start here: >> >> <https://developer.mozilla.org/en- >> US/docs/Web/JavaScript/Reference/Functions/Arrow_functions#Browser_compatibi >> lity> > > There is always the ECMAScript 2015 specification[1], but it's impossibly turgid in many places. MDN is becoming an increasingly important resource. > > 1. <http://ecma-international.org/ecma-262/6.0/index.html#sec-arrow-function-definitions> > The ECMAScript specification is now developed on GitHub, instead of a Word document: <https://github.com/tc39/ecma262/releases/tag/es2016-draft-1> The pdf version of the ES2015 specification contains 545 pages! FTR, Golang specification is far shorter than that. -- Joao Rodrigues
[toc] | [prev] | [next] | [standalone]
| From | donmaximo01@gmail.com |
|---|---|
| Date | 2015-10-29 06:08 -0700 |
| Message-ID | <231237fd-7168-4fa8-ab93-5e9802b84a76@googlegroups.com> |
| In reply to | #28553 |
FOR My Price and Pictures On the Pills / vals contact me at zita3322@gmail.com waiting
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.javascript
csiph-web