GithubHelp home page GithubHelp logo

each's Introduction

each

Array / object / string iteration utility.

Installation

$ component install component/each

API

each(array, fn[, ctx])

Iterate an array:

each([1,2,3], function(num, i){
  
})

Optionally pass a context object:

each([1,2,3], function(num, i){

}, this)

each(object, fn[, ctx])

Iterate an object's key / value pairs:

each(conf, function(key, val){
  
})

Iterate an array-ish object (has numeric .length):

each(collection, function(val, i){
  
})

each(string, fn[, ctx])

Iterate a string's characters:

each('hello', function(c, i){
  
})

License

MIT

each's People

Contributors

calvinfo avatar forbeslindesay avatar juliangruber avatar retrofox avatar stagas avatar timaschew avatar tj avatar tootallnate avatar yields avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

each's Issues

Question on the order of callback parameters for iteration over object.

Hi,
I'm a bit wondering why the order of the parameters are kinda reversed on object iteration. If there's any reason why the order is in this way, it would be helpful if you can share your thoughts in order for me and potentially others to avoid relevant mistakes and such.

Currently when you iterate over object, you will get key, (or property name) as the first argument:

var conf = {
  fast: 1,
  normal: 0.5,
  slow: 0.3
};
each(conf, function(key, val){
  console.log(val, key); // => 1, "fast"... 
})

But if we consider key to be more like index, the following order is a bit more consistent compared to other types, where val is always passed as first argument.

var conf = {
  fast: 1,
  normal: 0.5,
  slow: 0.3
};
each(conf, function(val, key){
  console.log(val, key);  // => 1, "fast"... 
})

each("something", function(val, index){
  console.log(val, index)  // => "s", 0... 
})

each(['a', 'b', 'c'], function(val, index){
  console.log(val, index); // => "a", 0…
})

underscore.js and lodash.js are passing val in the first parameter:

var _ = require('underscore') // require('lodash');
var conf = {
  fast: 1,
  normal: 0.5,
  slow: 0.3
};
_.each(conf, function(val, key){
  console.log(val, key); // => 1, "fast"... 
})

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.