查找嵌套数组的最大长度

时间:2016-06-25 15:36:22

标签: javascript arrays d3.js

我有一个包含节点和边缘内部边缘的数组,每个数组都有源,目标,数组人员,我想创建一个长度为数组的数组。

$(this).find('.videos').attr('src', '');

数组如何显示:

  var officersScale = d3.scale.linear()
                                        .domain([1,  d3.max(dataset.edges.officers.length, function(d) { return d[0]; })])
                                        .range([1,10]);

1 个答案:

答案 0 :(得分:3)

首先,您的数据无效。存在无效的令牌,某些内部对象缺少{等。

但是,您可以遍历所有人员阵列,然后找到最大长度。我希望你知道如何创建规模,这对你来说。

var edges = [{"source":0,"target":29,"officers":["MEDORI, Rene"]},
           {"source":6,"target":28,"officers":["GILLIES, Crawford Scott","GRIMSTONE, Gerald Edgar, Sir"]},
           {"source":9,"target":10,"officers":["ANDERSON, Paul Milton"]},
           {"source":9,"target":19,"officers":["FREW, Anita Margaret"]},
           {"source":9,"target":23,"officers":["BRINDED, Malcolm Arthur"]},
           {"source":15,"target":32,"officers":["KENNEDY, Christopher John"]},
           {"source":24,"target":31,"officers":["CHAMBERS, Stuart John"]},
           {"source":28,"target":29,"officers":["GILLIES, Crawford Scott"]},
           "source":30,"target":31,"officers":["GROTE, Byron Elmer"]}];

使用d3.max

var maxlength = d3.max(edges, function(d) { return d.officers.length; })

使用简单的Array.prototype.mapMath.max

var lengths = edges.map(function(edge){
     return edge.officers.length;
});

console.log(lengths);
var maxLength = Math.max(...lengths);

console.log(maxLength);

一步到位ES6

maxLength = Math.max(...edges.map(edge=> edge.officers.length));