Se si dispone di un array nidificato di numeri (arrays = [[1, 2], [20, 3]]
), nido d3.max:
var max = d3.max(arrays, function(array) {
return d3.max(array);
});
o equivalentemente, utilizzare :
var max = d3.max(arrays.map(function(array) {
return d3.max(array);
}));
Se si desidera ignorare s tring valori, è possibile utilizzare array.filter di ignorare le stringhe:
var max = d3.max(arrays, function(array) {
return d3.max(array.filter(function(value) {
return typeof value === "number";
}));
});
In alternativa, se si conosce la stringa è sempre in prima posizione, è possibile utilizzare array.slice che è un po 'più efficiente:
var max = d3.max(arrays, function(array) {
return d3.max(array.slice(1));
});
Un'altra opzione è usare una funzione accessor che restituisce NaN
per valori che non sono numeri. Ciò farà sì che d3.max ignori quei valori. Comodamente, di JavaScript incorporato Number
funzione fa esattamente questo, quindi si può dire:
var max = d3.max(arrays, function(array) {
return d3.max(array, Number);
});
Ho lo stesso problema ma quando provo ad applicare il filtro dell'array ma ho il seguente errore: TypeError: undefined non è una funzione (che valuta l'array.filtro (funzione (valore) \t \t { \t \t \t \t ritorno valore typeof === "numero"; \t \t}) '). La mia variabile di scena è una matrice di oggetti caricati da un json. Che c'è ? –