Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/363.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
分组Javascript对象并插入自定义值_Javascript - Fatal编程技术网

分组Javascript对象并插入自定义值

分组Javascript对象并插入自定义值,javascript,Javascript,我已经实现了以下几点 输入 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} ]; an[0]=[{color:"yellow",weight:[12,22]}] an[1]=[

我已经实现了以下几点

输入

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}
];
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]}]
电流输出

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}
];
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]}]
但是,如果该javascript对象数组中不存在数据,请在其中添加0

所需输出

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}
];
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]}]

进一步解释,
数据[1]
蓝色
颜色,但
数据[0]
因此没有,
权重=[0,10]
<代码>0来自
数据[0]
10
来自
数据[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];
    });     
};  
()

编辑:根据评论中的要求

第一部分仅准备输入结构中所有颜色的并集的输出对象


然后,在贴图迭代中,检查每个第一级实例是否存在颜色,并拾取权重或将其默认为0。然后将该值推送到权重数组中。

为什么不包括实际代码?以及默认情况下[0,0]各有哪些内容,只更新新值?请向我们显示JSFIDLE的预期输出。在一个数组中包含相同颜色的副本,这与您上面显示的情况非常不同。您的实际目标是什么?为什么不在第一篇文章中解释一下,让我们了解一下你到底想做什么?请描述一下你想要实现什么。@DavidThomas-老实说,我不认为这有用,但这是OP要求的。。。我只是简单地获取他的代码并“修复”它,以生成他想要的输出