Open In App

D3.js pairs() method

Improve
Improve
Like Article
Like
Save
Share
Report

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 iterable after passing them to the reducer function by using this method.

Javascript




// Defining d3 contrib variable
const d3 = require('d3');
 
const 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
const d3 = require('d3');
 
const 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 ]

Last Updated : 06 Apr, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads