Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

D3.js pairs() method

  • Last Updated : 14 Sep, 2020

With the help of d3.pairs() method, we can get the array from the iterables where adjacent elements that is “i”th element and (i-1)th element passes through the reducer function to get the output.

Syntax:

d3.pairs(iterable[, reducer])

Return value: It will return the single array of reduced pairs.

Note: To execute the below examples you have to install the d3 library by using the command prompt for the following command.

npm install d3

Example 1: In this example we can see that by using d3.pairs() method, we are able to get the single array having pairs from iterables after passing them to the reducer function by using this method.

Javascript




// Defining d3 contrib variable  
var d3 = require('d3');
  
var gfg = d3.pairs([1, 2, 3, 4, 5, 6], (a, b) => a + b);
  
console.log(gfg);

Output:

[ 3, 5, 7, 9, 11 ]

Example 2:

Javascript




// Defining d3 contrib variable  
var d3 = require('d3');
  
var gfg = d3.pairs
  ([1, 2, 3, 4, 5, 6], (a, b) => (a + b)/2);
  
console.log(gfg);

Output:

[ 1.5, 2.5, 3.5, 4.5, 5.5 ]
My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!