JavaScript Chain onto native Javascript functions

tl;dr:

How can I chain onto Javascript's map() with my own function? Like -

stuff.map(i => i.key).avg()

where avg() is my own function to compute the average of the array returned by map?


In moving away from objects and toward functional programming with pure functions, I've lost the handy

return this;

that allows me to chain.

If I have

let stuff = [
  {id: 1, name: 'tuan', country: 'VN', age: 23},
  {id: 2, name: 'nhung', country: 'US', age: 25},
  ...

//my own filter to pass as a param to native filter()
var filt = x => j => j.country === x;

//my own reducer for an array that computes an average
let avg = (arr) => (arr.reduce((acc, i) => acc + i) / arr.length);

then

stuff.filter(filt('VN')).map(i => i.age)

would return something like

[23, 34, 45]

but

stuff.filter(filt('VN')).map(i => i.age).avg()

gives an error like

filter().map().avg() is not a function 

How can we write functions that chain onto the native ones?

Answer:1

It should be avg(stuff.filter(filt('VN')).map(i => i.age)) because you defined the avg function that expects arr as its argument. You did not extend the Array prototype with the avg method.

Answer:2

Chaining isn't magic — you're just calling a method on the return value of a function. If the function doesn't support that method, you need to add it to the prototype.

This works because map returns and array and arrays have a join() method:

var a = [1, 2, 3, 4, 5]
a.map((i) => i *2 ).join(",")

But arrays don't have an avg() method unless you add it so the chaining won't work.

Answer:3

I am working on google bar charts but I am not able to change the background color of google barcharts. Here is my code: <script type="text/javascript"> google.load('visualization', '1', {...

I am working on google bar charts but I am not able to change the background color of google barcharts. Here is my code: <script type="text/javascript"> google.load('visualization', '1', {...

  1. change background google docs
  2. change background google slides
  3. change background google chrome
  4. change background google drawing
  5. change background google calendar
  6. change background google hangouts
  7. change background google slides app
  8. change background google sites
  9. change background google drive
  10. change background google search page
  11. change background google home hub
  12. change background google sheets
  13. change background google hub
  14. change background google slides ipad
  15. change background google earth
  16. change background google forms
  17. change background google chromecast
  18. change background google account

Table doesn't cover all div, but i give a width in 100% $("#astana").click(function(){ $(".t2").toggle(500); }); /*---tables---*/ .wrapper { margin: 0 auto; max-width: 800px; } ...

Table doesn't cover all div, but i give a width in 100% $("#astana").click(function(){ $(".t2").toggle(500); }); /*---tables---*/ .wrapper { margin: 0 auto; max-width: 800px; } ...

I am building my own type of Chrome dev tools (inspect element) which will also be in the form of a Chrome extension using JavaScript. I would like it to have some similar functionality to Chromes dev ...

I am building my own type of Chrome dev tools (inspect element) which will also be in the form of a Chrome extension using JavaScript. I would like it to have some similar functionality to Chromes dev ...

I have this line as a part from uml doc. <children xmi:type="corstr:heighj" uml:name="private" x="255" y="188" width="221" height="51"/> I need the values for x,y width and height * I tried ...

I have this line as a part from uml doc. <children xmi:type="corstr:heighj" uml:name="private" x="255" y="188" width="221" height="51"/> I need the values for x,y width and height * I tried ...

  1. file attributes from command line
  2. modify attributes from command line