ext.js and array filter

797 views
Skip to first unread message

francisco treacy

unread,
Apr 20, 2010, 7:16:50 AM4/20/10
to expre...@googlegroups.com
I am failing to do something very simple in my express app: filter an array.

var match = '9415216315537691'
var a = ... // an array of 2 objects

sys.p(a)

[ { id: '3627807730808854'
, start: [ 3, 5, 1 ]
, end: [ 3, 5, 7 ]
}
, { id: '9415216315537691'
, start: [ 3, 8, 1 ]
, end: [ 3, 8, 11 ]
}
]

// filter out one object, matching by id
var result = a.filter(function(h) { return h.id !== match });
sys.p(result) // the result is correct, but what's that format?

[ { id: '3627807730808854'
, start: [ 3, 5, 1 ]
, end: [ 3, 5, 7 ]
}
, length: 1
]

sys.puts(JSON.stringify(result))

{"0":{"id":"3627807730808854","start":[3,5,1],"end":[3,5,7]},"length":1}


It's actually ext.js and its monkey patches driving me crazy...lol...
Forcing it on the client is intended? Maybe I cannot see the
advantage.

My hack is to do: var result = [];
result.push(a.filter(function(h) { return h.id !== match })[0]); --
but can we get back the good ol' [].filter working properly?

Thanks guys, Francisco

--
You received this message because you are subscribed to the Google Groups "Express" group.
To post to this group, send email to expre...@googlegroups.com.
To unsubscribe from this group, send email to express-js+...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/express-js?hl=en.

Aaron Heckmann

unread,
Apr 20, 2010, 8:15:05 AM4/20/10
to expre...@googlegroups.com
Removing ext as a dependency is on the Github Issues list. I'm for removing it as well. 
TJ, what do you think?
--
Aaron

vision media [ Tj Holowaychuk ]

unread,
Apr 20, 2010, 11:11:38 AM4/20/10
to expre...@googlegroups.com
it should work fine with your Array... we use Object.create(Object.getPrototypeOf(this))
so you can filter on an Object, and have it return an Object not an Array.


there are quite a few things that I use from ext currently in express but it would not
be a huge deal to remove it, although im not seeing how your getting that problem at all. JavaScript's
filter() method is pretty much the same as ruby's #select. If you JSON.stringify() the array then
its going to look like an object

--
Tj Holowaychuk
Vision Media
President & Creative Lead

francisco treacy

unread,
Apr 20, 2010, 12:18:58 PM4/20/10
to expre...@googlegroups.com
Hmm ok! I want to filter an array of objects [{}, {}, ...] and get
another [{}, {}] as an output.

But all i'm trying to say is that the same code behaves differently in
the REPL than in an express webapp (because of the inclusion of
ext.js, I presume).

And the REPL yields what I expect. (Where does that length property
come from anyway?)


2010/4/20 vision media [ Tj Holowaychuk ] <t...@vision-media.ca>:

vision media [ Tj Holowaychuk ]

unread,
Apr 20, 2010, 12:32:08 PM4/20/10
to expre...@googlegroups.com
In the repl I get an array of objects as well ... when your filter is !== someId your expecting one
object back? not an array? maybe you want Array#detect()

I did find out that apparently the macro in V8 IS_ARRAY does not work with Object.create(Object.getPrototypeOf([]))
which is what I use for the Enumerable mixin to handle objects / arrays. It is still an Array,
so its strange, but its just a falt in how JSON.stringify() renders output, thats all

francisco treacy

unread,
Apr 20, 2010, 12:40:08 PM4/20/10
to expre...@googlegroups.com
node> var a = [ {id: 1}, {id: 2}, {id: 3} ].filter(function(n) {
return n.id !== 2 })
[ { id: 1 }, { id: 3 } ]
node> JSON.stringify(a)
'[{"id":1},{"id":3}]'

This is all I want.

When I do *exactly the same* in an express webapp, I get

{"0":{"id":1},"1":{"id":3},"length":2}

No idea if the problem lies in node or v8!

Thanks =)

vision media [ Tj Holowaychuk ]

unread,
Apr 20, 2010, 12:52:38 PM4/20/10
to expre...@googlegroups.com
oh, I get it, you actually need the JSON output. I thought you were just using that
to convey your confusion :) in that case it IS v8, however from checking the c++ there
is not going to be a way around it that i can see. Im not really sure why it considers the two
ways of creating an array different. 

I will change the ext methods #detect and #filter to return an array all the time instead of
using Object.create() at all, will save you this trouble :) 

Jesse Hallett

unread,
Apr 20, 2010, 12:55:15 PM4/20/10
to expre...@googlegroups.com

When you use `Object.create(Object.getPrototypeOf([]))` the resulting object has the same prototype as an array; but it is not a true array because the object was not produced by the Array() constructor.

I believe that the JSON serialization in this case is technically correct - even though it does not produce the result that Francisco would like.

Maybe you could modify your Enumerable mixin to skip adding methods to objects that already implement a method with the same name?  Replacing native methods is likely to cause lots of weird issues.

On Apr 20, 2010 9:40 AM, "francisco treacy" <francisc...@gmail.com> wrote:

node> var a = [ {id: 1}, {id: 2}, {id: 3} ].filter(function(n) {
return n.id !== 2 })
[ { id: 1 }, { id: 3 } ]
node> JSON.stringify(a)
'[{"id":1},{"id":3}]'

This is all I want.

When I do *exactly the same* in an express webapp, I get

{"0":{"id":1},"1":{"id":3},"length":2}

No idea if the problem lies in node or v8!

Thanks =)



2010/4/20 vision media [ Tj Holowaychuk ] <t...@vision-media.ca>:

> In the repl I get an array of ob...

--

You received this message because you are subscribed to the Google Groups "Express" group.

To post t...

vision media [ Tj Holowaychuk ]

unread,
Apr 20, 2010, 1:07:14 PM4/20/10
to expre...@googlegroups.com
For sure, I understand that, but you would think they could just do an "instanceof Array" check.
--
Tj Holowaychuk
Vision Media
President & Creative Lead

Jesse Hallett

unread,
Apr 20, 2010, 1:22:32 PM4/20/10
to expre...@googlegroups.com

Well, if a JSON implentation used instanceof to check for an array it would not be in compliance with the JSON spec.  It is not an object's prototype that makes it an array.  It is the Array() constructor that does that.

To generalize on that point, you could also point to examples of user defined objects where an object cannot be treated as the same type as another object with the same prototype but that was created by a different constructors.  Constructors populate required properties and define priviliged methods and so forth.

On Apr 20, 2010 10:07 AM, "vision media [ Tj Holowaychuk ]" <t...@vision-media.ca> wrote:

For sure, I understand that, but you would think they could just do an "instanceof Array" check.

On Tue, Apr 20, 2010 at 9:55 AM, Jesse Hallett <hall...@gmail.com> wrote:

>
> When you use `Object.create(Object.getPrototypeOf([]))` the resulting object has the same protot...

--

> You received this message because you are subscribed to the Google Groups "Express" group.

> To post to this group, send email to expre...@googlegroups.com.

> To unsubscribe from this group...





--
Tj Holowaychuk
Vision Media
President & Creative Lead

--

You received this message because you are subscribed to the Google Groups "Express" group.

To post to this group, send email to expre...@googlegroups.com.

To unsubscribe from this group, se...

vision media [ Tj Holowaychuk ]

unread,
Apr 20, 2010, 1:23:32 PM4/20/10
to expre...@googlegroups.com
true true
Reply all
Reply to author
Forward
0 new messages