Closed. This question does not meet Stack Overflow guidelines。它当前不接受答案。
                        
                    
                
            
        
            
        
                
                    
                
            
                
                    想改善这个问题吗? Update the question,所以它是on-topic,用于堆栈溢出。
                
                    6年前关闭。
            
        

    

我想将多个数组展平为单个对象。谁能从中找到映射或旋转数据的好方法:

Something: [
  {"Test":0,"Count":206,"Abc":0},
  {"Test":2,"Count":119,"Abc":3},
  {"Test":1,"Count":207,"Abc":3},
  {"Test":3,"Count":129,"Abc":8,
  {"Test":4,"Count":122,"Abc":2}
]


对此吗?

{
  Name: "Something",
  Test0: 206,
  Test1: 207,
  Test2: 119,
  Test3: 129,
  Test4: 122
}

最佳答案

Array.prototype.flattenOn=function(out,key,value)
{
    for(var i=0,l=this.length;i<l;i++)
    {
        out[key+''+this[i][key]]=this[i][value];
    }
    return out;
};

var obj={
  Something:[
  {"Test":0,"Count":206,"Abc":0},
  {"Test":2,"Count":119,"Abc":3},
  {"Test":1,"Count":207,"Abc":3},
  {"Test":3,"Count":129,"Abc":8},
  {"Test":4,"Count":122,"Abc":2}
  ]
};

var output={};
output.Name='Something';
output=obj.Something.flattenOn(output,'Test','Count');
alert(JSON.stringify(output));


以下是同样的小提琴。
http://jsfiddle.net/67aXK/

10-06 05:05