转换具有相同键值的JSON数组

时间:2017-09-14 10:36:58

标签: javascript jquery html angularjs frontend

我有一个这种形式的数组

[
        {
            "time": "2017-09-14 02:44 AM",
            "artist": "Sam",
            "message": "message 1",
            "days": 0
        },
         {
            "time": "2017-09-14 02:44 AM",
            "artist": "Jerry",
            "message": "message 2",
            "days": 2
        },
        {
            "time": "2017-09-14 02:44 AM",
            "artist": "Sam",
            "message": "message 3",
            "days": 4
        },
         {
            "time": "2017-09-14 02:49 AM",
            "artist": "Jerry",
            "message": "message 5",
            "days": 2
        },
         {
            "time": "2017-09-14 02:59 AM",
            "artist": "Sam",
            "message": "message 7",
            "days": 10
        },
        {
            "time": "2017-09-14 02:59 AM",
            "artist": "Sam",
            "message": "message 5",
            "days": 10
        },
]

我需要像这样输出

[{
    artist:"Sam",
    data : [
        {message:"message 1" , days: 0},
        {message:"message 3" , days: 4},
        {message:"message 7" , days: 10},
        {message:"message 5" , days: 10}
    ]
},
{
    artist:"Jerry",
    data : [
        {message: "message 2", days: 2},
        {message: "message 5", days: 2}
    ]
}]

非常感谢您的任何帮助。

这是我试过的代码,

  var obj = {};
    data.map(function(item){
        if(angular.isDefined(obj.artist) && obj.artist === item.artist){
            obj.data.push({message: item.message, days:item.days })
        }else{
            obj.artist = item.artist;
            obj.data = [];
            obj.data.push({message: item.message, days:item.days })
        }
    });
   console.log(obj);

但它会创建一个JSON对象。

2 个答案:

答案 0 :(得分:3)

试试 Array#reduce 功能

  1. 使用新阵列推送created obj
  2. 然后仅使用artist值创建数组。
  3. 然后应用于检查已存在于数组中的艺术家值或不使用 Array#indexOf()>0

  4. 在那里。只需将另一个对象推送到存在的数据数组

  5. Array#index用于查找新数组中提供的对象的位置
  6. 
    
    var arr = [ { "time": "2017-09-14 02:44 AM", "artist": "Sam", "message": "message 1", "days": 0 }, { "time": "2017-09-14 02:44 AM", "artist": "Jerry", "message": "message 2", "days": 2 }, { "time": "2017-09-14 02:44 AM", "artist": "Sam", "message": "message 3", "days": 4 }, { "time": "2017-09-14 02:49 AM", "artist": "Jerry", "message": "message 5", "days": 2 }, { "time": "2017-09-14 02:59 AM", "artist": "Sam", "message": "message 7", "days": 10 }, { "time": "2017-09-14 02:59 AM", "artist": "Sam", "message": "message 5", "days": 10 }, ]
    
    
    var res = arr.reduce(function(a,b){
    var f =a.map(i=> i.artist)
    if(f.indexOf(b.artist) > 0){
      a[f.indexOf(b.artist)].data.push({message:b.message,days:b.days})
    }else{
    a.push({artist:b.artist, data:[{message:b.message,days:b.days}]})
    }
    return a;
    },[])
    
    console.log(res)
    
    
    

答案 1 :(得分:2)

您可以为同一位艺术家使用哈希表并使用它收集数据。



var data = [{ time: "2017-09-14 02:44 AM", artist: "Sam", message: "message 1", days: 0 }, { time: "2017-09-14 02:44 AM", artist: "Jerry", message: "message 2", days: 2 }, { time: "2017-09-14 02:44 AM", artist: "Sam", message: "message 3", days: 4 }, { time: "2017-09-14 02:49 AM", artist: "Jerry", message: "message 5", days: 2 }, { time: "2017-09-14 02:59 AM", artist: "Sam", message: "message 7", days: 10 }, { time: "2017-09-14 02:59 AM", artist: "Sam", message: "message 5", days: 10 }],
    hash = Object.create(null),
    result = data.reduce(function (r, a) {
        if (!hash[a.artist]) {
            hash[a.artist] = { artist: a.artist, data: [] };
            r.push(hash[a.artist]);
        }
        hash[a.artist].data.push({ message: a.message, days: a.days });
        return r;
    }, []);

console.log(result);

.as-console-wrapper { max-height: 100% !important; top: 0; }