geeks
geeks

Reputation: 2055

What is use of $.map function in javascript?

Note: I just want to to understand what is $.map doing in following code..

I am working on openstack horizon,In one of the javascript file they are using $.map function Please seehorizon.d3linechar.js

My question is how $.map is works, what is $ before map. $.map is associated with javascript or jquery..

$.map(self.series, function(serie) {
        serie.color = last_point_color = self.color(serie.name);
        $.map(serie.data, function(statistic) {
          // need to parse each date
          statistic.x = d3.time.format.utc('%Y-%m-%dT%H:%M:%S').parse(statistic.x);
          statistic.x = statistic.x.getTime() / 1000;
          last_point = statistic;
          last_point.color = serie.color;
        });
      });

Upvotes: 0

Views: 133

Answers (2)

Michael W. Czechowski
Michael W. Czechowski

Reputation: 3457

Please read the jQuery documentation. Their are many many examples. Our folk is realy trying to help you. But what is the lack of your understanding in the $.map() function?

$ is only the namespace and makes at least the map function work. So forget about it.

map( input, outputFunction ) is iterating through the input which has to be an real array. The outputFunction, usually a self executing function, is able to manipulate the content of each element of the inputed array.

In your example:

$.map(self.series, function(serie) {

self.series is the input and each element of that array will be called as serie in the anonymous or rather self executed function.

serie.color = last_point_color = self.color(serie.name);

Change some color stuff...

$.map(serie.data, function(statistic) {

Next call of the mapping function.

  // need to parse each date
  statistic.x = d3.time.format.utc('%Y-%m-%dT%H:%M:%S').parse(statistic.x);

Parsing the date to a specific format like discribed in the comment.

  statistic.x = statistic.x.getTime() / 1000;

Take the x value parse to a time or maybe to seconds and divide through 1000.

  last_point = statistic;

Save element of serie.data to a temporar variable.

  last_point.color = serie.color;

Save the color of the of the serie to the element of that serie.

    });
  });

All in all... ... $.map() iterates through self.series, then iterates through its children and then it looks like it changes the color of every single element to the color of that series.

Upvotes: 1

AmmarCSE
AmmarCSE

Reputation: 30557

$ is an alias for the jQuery object.

As for map(), it is an api function in jQuery used to convert the items in an array.

If you look at the source for map, the basic algorithm is to iterate over the passed in elems and obtain a converted value using the callback

function (elems, callback) {
    var value, i = 0,
        length = elems.length,
        ret = [];

      for (; i < length; i++) {
      //alternatively, for objects,  for (i in elems) {
            value = callback(elems[i], i);

            if (value != null) {
                ret.push(value);
            }
        }
    // Flatten any nested arrays
    return concat.apply([], ret);
}

Upvotes: 0

Related Questions