Skip to content

数据变换

¥Transforming data

Transform arrays and generate new arrays.

cross(...iterables, reducer)

示例 · 源代码 · 返回指定可迭代对象的 笛卡尔积

¥Examples · Source · Returns the Cartesian product of the specified iterables.

js
d3.cross([1, 2], ["x", "y"]) // [[1, "x"], [1, "y"], [2, "x"], [2, "y"]]

如果指定了 Reducer,则对每个给定可迭代对象中的每个元素组合调用该 Reducer,并返回相应的 Reducer 值。

¥If a reducer is specified, it is invoked for each combination of elements from each of the given iterables, and returns the corresponding reduced value.

js
d3.cross([1, 2], ["x", "y"], (a, b) => a + b) // ["1x", "1y", "2x", "2y"]

merge(iterables)

示例 · 源代码 · 将指定的可迭代对象合并为一个新的平面数组。此方法类似于内置的 array.concat 方法,但当你拥有一个数组的数组或一个可迭代对象的迭代器时更方便。

¥Examples · Source · Merges the specified iterable of iterables into a new flat array. This method is similar to the built-in array.concat method, but is more convenient when you have an array of arrays or an iterable of iterables.

js
d3.merge([[1], [2, 3]]) // [1, 2, 3]
js
d3.merge(new Set([new Set([1]), new Set([2, 3])])) // [1, 2, 3]

pairs(iterable, reducer)

示例 · 源代码 · 按顺序返回由指定可迭代对象中的相邻元素对组成的数组。如果指定的可迭代对象少于两个元素,则返回空数组。

¥Examples · Source · Returns an array of adjacent pairs of elements from the specified iterable, in order. If the specified iterable has fewer than two elements, returns the empty array.

js
d3.pairs([1, 2, 3, 4]) // [[1, 2], [2, 3], [3, 4]]

如果指定了 Reducer 函数,则依次向其传递一个元素 i。 - 1 和来自可迭代对象的元素 i。

¥If a reducer function is specified, it is successively passed an element i - 1 and element i from the iterable.

js
d3.pairs([1, 1, 2, 3, 5], (a, b) => b - a) // [0, 1, 1, 2]

transpose(matrix)

示例 · 源代码 · 将 zip 运算符用作二维 矩阵转置

¥Examples · Source · Uses the zip operator as a two-dimensional matrix transpose.

js
d3.transpose([["Alice", "Bob", "Carol"], [32, 13, 14]]) // [["Alice", 32], ["Bob", 13], ["Carol", 14]]
js
d3.transpose([["Alice", 32], ["Bob", 13], ["Carol", 14]]) // [["Alice", "Bob", "Carol"], [32, 13, 14]]

zip(...arrays)

示例 · 源代码 · 返回一个数组的数组,其中第 i 个数组包含每个参数数组的第 i 个元素。返回的数组长度将被截断为数组中最短的数组。如果数组仅包含一个数组,则返回的数组包含一个元素数组。如果没有参数,则返回空数组。

¥Examples · Source · Returns an array of arrays, where the ith array contains the ith element from each of the argument arrays. The returned array is truncated in length to the shortest array in arrays. If arrays contains only a single array, the returned array contains one-element arrays. With no arguments, the returned array is empty.

js
d3.zip(["Alice", "Bob", "Carol"], [32, 13, 14]) // [["Alice", 32], ["Bob", 13], ["Carol", 14]]

filter(iterable, test)

源代码 · 返回一个新数组,其中包含来自可迭代对象的映射值,按给定映射函数定义的顺序排列。

¥Source · Returns a new array containing the values from iterable, in order, for which the given test function returns true.

js
d3.filter(new Set([0, 2, 3, 4]), (d) => d & 1) // [3]

类似 array.filter,但可与任何可迭代对象配合使用。

¥Like array.filter, but works with any iterable.

map(iterable, mapper)

源代码 · 返回一个新数组 [x, y](通常以像素为单位),表示给定点的投影点。

¥Source · Returns a new array containing the mapped values from iterable, in order, as defined by given mapper function.

js
d3.map(new Set([0, 2, 3, 4]), (d) => d & 1) // [0, 0, 1, 0]

类似 array.map,但可与任何可迭代对象配合使用。

¥Like array.map, but works with any iterable.

reduce(iterable, reducer, initialValue)

源代码 · 返回由给定 reducer 函数定义的约简值,该函数会针对可迭代对象中的每个值重复调用,并传递当前约简值和下一个值。

¥Source · Returns the reduced value defined by given reducer function, which is repeatedly invoked for each value in iterable, being passed the current reduced value and the next value.

js
d3.reduce(new Set([0, 2, 3, 4]), (p, v) => p + v, 0) // 9

类似 array.reduce,但可与任何可迭代对象配合使用。

¥Like array.reduce, but works with any iterable.