我已实施以下
https://jsfiddle.net/f3syta4f/2/
<SELECT NAME="CB" id="CBid" class ="CBclass" >
<option value="ABC" >ABC</option>
<option value="MCD" >MCD</option>
<option value="XYZ" >XYZ</option>
</SELECT>
<INPUT id="Edit" TYPE="button" VALUE="Edit" onClick="EditName()" />
输入
function indexByColor (input) {
var output = {};
for (var i in input) {
for (var j in input[i]) {
var x = input[i][j];
if (output[x.color] === undefined) output[x.color] = {
color: x.color,
weight: [],
};
output[x.color].weight.push(x.weight);
};
};
return Object.keys(output).map(function(c){
return output[c];
});
};
当前输出
dataSet[0]= [
{color:"yellow",weight:12,key:0}
,{color:"red",weight:15,key,0}
];
dataSet[1]= [
{color:"yellow",weight:22,key:1}
,{color:"blue",weight:10,key:1}
];
但是,如果该javascript对象数组中不存在数据,则在其中添加0。
所需输出
an[0]=[{color:"yellow",weight:[12,22]}]
an[1]=[{color:"red",weight:[15]}]
an[2]=[{color:"blue",weight:[10]}]
进一步说明,an[0]=[{color:"yellow",weight:[12,22]}]
an[1]=[{color:"red",weight:[15,0]}]
an[2]=[{color:"blue",weight:[0,10]}]
有data[1]
颜色,blue
因此data[0]
.. weight=[0,10]
来自0
,{{ 1}}来自data[0]
答案 0 :(得分:1)
这对你有用:
function indexByColor (input) {
var output = {};
for (var i in input) {
for (var j in input[i]) {
var x = input[i][j];
output[x.color] = {
color: x.color,
weight: [],
};
};
};
return Object.keys(output).map(function(c){
for(var i in input) {
var weight = 0;
for(var j in input[i])
if(input[i][j].color == c) {
weight = input[i][j].weight;
break;
}
output[c].weight.push(weight);
}
return output[c];
});
};
(Fiddle)
编辑:根据评论中的要求......
第一部分仅使用输入结构中所有颜色的并集来准备输出对象。
然后,在地图迭代中,检查每个第1级实例是否存在颜色,并选择权重或默认为0.然后将该值推入权重数组。