Home Convert JSON array with same key value
Reply: 2

Convert JSON array with same key value

Ankit Agarwal
1#
Ankit Agarwal Published in 2017-09-14 10:36:57Z

I have a array in this form

[
        {
            "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
        },
]

and i need output as like this

[{
    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}
    ]
}]

Any help from you is highly appreciated.

here is the code i have tried,

  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);

But it creates a single JSON object.

Nina Scholz
2#
Nina Scholz Reply to 2017-09-14 10:58:52Z

You could use a hash table for same artist and collect the data with it.

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

prasanth
3#
prasanth Reply to 2017-09-14 11:02:23Z

Try this Array#reduce function

  1. push created obj with the new array.
  2. Then create the array only with artist value.
  3. Then apply to check artist value already present in array or not using Array#indexOf()>0.

  4. is there .just push the other object to exist data array

  5. Array#index used find the position of the object presented in new array

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)

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.308013 second(s) , Gzip On .

© 2016 Powered by cudou.com design MATCHINFO