3

I have found out that you can use jQuery with node.js but all examples are for DOM and HTML manipulation.

Do you think using it for array iterations ( for each ) etc would be ok or a bit overkill?

0

5 Answers 5

14

Most of the common jQuery utilities are already in implemented in the V8 engine, which node.js is built on.

for example, compare:

One of the best things about node.js is that most of the ES5 spec is already there.

Sign up to request clarification or add additional context in comments.

4 Comments

It's not because of the links, it's because you haven't learned to use markdown yet.
well, when i tried to submit the original answer it said i don't have enough rep to post more than two links. thanks for cleaning it up though!
ah, in that case, i've updated your answer to be linked correctly as they were legit.
Just to point out. At least presently $.map works with objects too. So it's not the same as using Array::map.
11

Underscore.js is a much smaller utility library for manipulating objects.

http://documentcloud.github.com/underscore/

npm install underscore

EDIT:

However, node.js has much better support for ES5 than browsers, and it's likely that you may not even need a library for manipulating objects. See keeganwatkins' answer.

3 Comments

You really should double check you need underscore, ES5 can do most of the what underscore can.
That's true, but stuff like _.extend isn't in ES5, and underscore uses the native ES5 if it's available.
Object.keys(source).forEach(function(key) { target[key] = source[key]; }); You don't need extend
3

NodeJS already has all the EcmaScript 5 Array Extras builtin. For example if you want all the odd squares:

[1,2,3,4,5,6,7,8,9].map(function(n){
    return n*n;
}).filter(function(n){
    return n%2;
});
//-> [1, 9, 25, 49, 81]

If you would like to see the other methods on the arrays, you can go to my JavaScript Array Cheat Sheet.

If you want the sum of all the cubes, you can:

[1,2,3,4,5,6,7,8,9].map(function(n){
    return n * n * n;
}).reduce(function(p, n){
    return p + n;
});
//-> 2025

Comments

0

jQuery has some nifty non-DOM features you can borrow -

https://github.com/jquery/jquery/tree/master/src

Just strip out what you do not need (like a reference to window).

Comments

0

If there are just a few functions you want to add, an alternative to adding a full library is to just implement the functions you want:

Many functions that are partially supported on some browsers are documented in MDN with compatibility code that can be used to add the function: Array.forEach

1 Comment

I don't know Node.js that well, if it already has forEach then why would it need to be added at all via jQuery?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.