# 数组

Howard Liang edited this page Nov 27, 2015 · 8 revisions

WikiAPI--中文手册核心函数数组

JavaScript包含修改数组的赋值方法(mutator methods)

• array.filter - 由满足特定条件的元素创建一个新的数组。
• array.forEach - 为数组中每一个元素调用一个函数。
• array.every - See if every element in the array satisfies a predicate.
• array.map - Create a new array with the result of calling a function on every element in the array.
• array.some - See if at least one element in the array satisfies a predicate.
• array.reduce - Apply a function to reduce the array to a single value (from left-to-right).
• array.reduceRight - Apply a function to reduce the array to a single value (from right-to-left).

## 排序 （Ordering）

# d3.ascending(a, b)

```function ascending(a, b) {
return a < b ? -1 : a > b ? 1 : a >= b ? 0 : NaN;
}```

# d3.descending(a, b)

```function descending(a, b) {
return b < a ? -1 : b > a ? 1 : b >= a ? 0 : NaN;
}```

# d3.min(array[, accessor])

# d3.max(array[, accessor])

# d3.extent(array[, accessor])

# d3.sum(array[, accessor])

# d3.mean(array[, accessor])

# d3.median(array[, accessor])

# d3.quantile(numbers, p)

```var a = [0, 1, 3];
d3.quantile(a, 0); // return 0
d3.quantile(a, 0.5); // return 1
d3.quantile(a, 1); // return 3
d3.quantile(a, 0.25); // return 0.5
d3.quantile(a, 0.75); // return 2
d3.quantile(a, 0.1); // return 0.19999999999999996 ```

# d3.variance(array[, accessor])

# d3.deviation(array[, accessor])

# d3.bisectLeft(array, x[, lo[, hi]])

# d3.bisect(array, x[, lo[, hi]]) # d3.bisectRight(array, x[, lo[, hi]])

# d3.bisector(accessor)
# d3.bisector(comparator)

```var data = [
{date: new Date(2011,  1, 1), value: 0.5},
{date: new Date(2011,  2, 1), value: 0.6},
{date: new Date(2011,  3, 1), value: 0.7},
{date: new Date(2011,  4, 1), value: 0.8}
];```

`var bisect = d3.bisector(function(d) { return d.date; }).right;`

`var bisect = d3.bisector(function(a, b) { return a.date - b.date; }).right;`

# d3.shuffle(array[, lo[, hi]])

## 关联数组 （Associative Arrays）

# d3.keys(object)

# d3.values(object)

# d3.entries(object)

`d3.entries({foo: 42， bar: true}); // returns [{key: "foo", value: 42}]`

### 映射（Maps）

# d3.map([object][, key])

```var m = d3.map([{name: "foo"}, {name: "bar"}], function(d) { return d.name; });
m.get("foo"); // {"name": "foo"}
m.get("bar"); // {"name": "bar"}
m.get("baz"); // undefined```

# map.has(key)

# map.get(key)

# map.set(key, value)

# map.remove(key)

# map.keys()

# map.values()

# map.entries()

# map.forEach(function)

# map.empty()

# map.size()

### 集合（Sets）

# d3.set([array])

# set.has(value)

# set.add(value)

# set.remove(value)

# set.values()

`d3.set(["foo", "bar", "foo", "baz"]).values(); // "foo", "bar", "baz"`

# set.forEach(function)

# set.empty()

# set.size()

## 数组运算符 (Array Operators)

# d3.merge(arrays)

`d3.merge([ [1], [2, 3] ]); // returns [1, 2, 3]`

# d3.range([start, ]stop[, step])

# d3.permute(array, indexes)

```var cols = ["site", "variety", "yield"];
thead.selectAll('th').data(cols)
.enter().append('th').text(function (d) { return d.toUpperCase(); });
tbody.selectAll('tr').data(yields)
.enter().append('tr').selectAll('td').data(function (row) { return d3.permute(row, cols); })
.enter().append('td').text(function (d) { return d; });```

# d3.zip(arrays…)

`d3.zip([1, 2], [3, 4]); // returns [[1, 3], [2, 4]]`

# d3.transpose(matrix)

# d3.pairs(array)

`d3.pairs([1, 2, 3, 4]); // returns [[1, 2], [2, 3], [3, 4]]`

### 嵌套 (Nest)

```var yields = [{yield: 27.00, variety: "Manchuria", year: 1931, site: "University Farm"},
{yield: 48.87, variety: "Manchuria", year: 1931, site: "Waseca"},
{yield: 27.43, variety: "Manchuria", year: 1931, site: "Morris"}, ...]```

```var nest = d3.nest()
.key(function(d) { return d.year; })
.key(function(d) { return d.variety; })
.entries(yields);```

```[{key: 1931, values: [
{key: "Manchuria", values: [
{yield: 27.00, variety: "Manchuria", year: 1931, site: "University Farm"},
{yield: 48.87, variety: "Manchuria", year: 1931, site: "Waseca"},
{yield: 27.43, variety: "Manchuria", year: 1931, site: "Morris"}, ...]},
{key: "Glabron", values: [
{yield: 43.07, variety: "Glabron", year: 1931, site: "University Farm"},
{yield: 55.20, variety: "Glabron", year: 1931, site: "Waseca"}, ...]}, ...]},
{key: 1932, values: ...}]```

# d3.nest()

# nest.key(function)

# nest.sortKeys(comparator)

```var nest = d3.nest()
.key(function(d) { return d.year; })
.sortKeys(d3.ascending)
.entries(yields);```

# nest.sortValues(comparator)

# nest.rollup(function)

# nest.map(array[, mapType])

```var yieldsByYearAndVariety = d3.nest()
.key(function(d) { return d.year; })
.key(function(d) { return d.variety; })
.map(yields, d3.map);```

# nest.entries(array)

[1] accessor function, 亦为getter, callback, 访问器, 常译作‘回调（函数）’。术语翻译 (计算机软件/编程) // Howard Liang 注 Nov 27, 2015

• Carry on 、2014-3-29翻译
• 咕噜2014-11-18翻译，并校对之前的翻译。

# Data-Driven Documents

### Translations (Unofficial)

##### Clone this wiki locally
You can’t perform that action at this time.
Press h to open a hovercard with more details.