DoneJS StealJS jQuery ++ FuncUnit DocumentJS
3.0.0
2.3.27

 

  • Github
  • Twitter
  • Chat
  • Forum
  • Guides
  • Core
  • Ecosystem
  • Infrastructure
  • Legacy
    • can-ejs
    • can-list
      • Prototype
        • attr
        • each
        • filter
        • map
        • reverse
        • splice
      • Static
        • Map
        • extend
    • can-map
    • can-map-backup
    • can-map-define
    • can-view-href
  • Bitovi
    • Bitovi.com
    • Blog
    • Consulting
    • Training
    • Open Source
  • Chat
  • Forum
  • Star
  • Follow @canjs
  • CanJS
  • /
  • Legacy
  • /
  • can-list
  • /
  • map
  • / On this page
    • map

      function

      Call a function on each element of a List and return a new List instance from the results.

      • source

      list.map( callback(item, index, listReference), context )

      Parameters

      1. callback {function(*, Number, can.List)}:

        A function to call with each element of the list.

      2. context {Object}:

        An optional object to use as this inside the callback.

      Returns

      {can.List}:

      A new can.List instance.

      var list = new List([1, 10, 100, 1000, 10000, 100000]);
      var newList = list.map(function(element, index, listReference) {
        var result;
      
        switch(index) {
          case 0: {
            result = false;
            break;
          }
          case 1: {
            result = undefined;
            break;
          }
          case 2: {
            result = element;
            break;
          }
          case 3: {
            result = element * 5;
            break;
          }
          default: {
            result = listReference[index] /= 2;
            break;
          }
        }
      
        return result;
      });
      
      console.log(list);    // [    1,        10, 100, 1000, 5000, 50000]
      console.log(newList); // [false, undefined, 100, 5000, 5000, 50000]
      

      CanJS is part of DoneJS. Created and maintained by the core DoneJS team and Bitovi. Currently 3.0.0.