2017-12-27 19 views
0

私はスプリングブートを使用しています。私はjsonオブジェクトを生成しました。JavaScriptの異なるjsonオブジェクト

[ 
    {"date":"2017-11-20T17:12:01.340","name":"abc","feedback":"good","category":"AA"}, 
    {"date":"2017-11-20T17:12:01.340","name":"abc","feedback":"bad","category":"BB"}, 
    {"date":"2017-11-20T17:12:01.340","name":"abc","feedback":"poor","category":"CC"} 

    {"date":"2017-12-15T16:53:02.042","name":"xyz","feedback":"very bad","category":"AA"} 
    {"date":"2017-12-15T16:53:02.042","name":"xyz","feedback":"nice","category":"XX"} 
    {"date":"2017-12-15T16:53:02.042","name":"xyz","feedback":"okey","category":"YY"} 
] 

私はJavaやJavaScriptでこのフォーマットを取得しようとする明確なに持っていると

[ 
    {"date":"2017-11-20T17:12:01.340", "name":"abc", "feedback_1":"good", "feedback_2":"bad", "feedback_3":"poor", "category_1":"AA", "category_2":"BB", "category_3":"CC"}, 
    {"date":"2017-12-15T16:53:02.042", "name":"xyz", "feedback_1":"very bad", "feedback_2":"nice", "feedback_3":"poor", "category_1":"AA", "category_2":"XX", "category_3":"YY"} 
] 

を、次のような新しいJSONオブジェクトを取得します。私はオンラインリソースを使って最善を尽くしましたが、JavaScriptを初めて使ったので失敗しました。どのように解決できますか?前もって感謝します。

+0

は、すでに試みているものを私達を表示... – void

答えて

0

は2列

1st array={"date":"2017-11-20T17:12:01.340","name":"abc","feedback":"good","category":"AA"}, 
{"date":"2017-11-20T17:12:01.340","name":"abc","feedback":"bad","category":"BB"}, 
{"date":"2017-11-20T17:12:01.340","name":"abc","feedback":"poor","category":"CC"} 

2nd={"date":"2017-12-15T16:53:02.042","name":"xyz","feedback":"very bad","category":"AA"} 
{"date":"2017-12-15T16:53:02.042","name":"xyz","feedback":"nice","category":"XX"} 
{"date":"2017-12-15T16:53:02.042","name":"xyz","feedback":"okey","category":"YY"} 

を取ると、一意の値

1

あなたがオブジェクトを作成し、JSONの周波数の記録を保持するカウンタを追加するためにarray#reduceを使用することができますを取得するためにarray_uniqueを使用し、このカウンタを使用してfeedbackを追加しますおよびcategory。その後、結果からカウンタを削除します。

var data = [ {"date":"2017-11-20T17:12:01.340","name":"abc","feedback":"good","category":"AA"}, {"date":"2017-11-20T17:12:01.340","name":"abc","feedback":"bad","category":"BB"}, {"date":"2017-11-20T17:12:01.340","name":"abc","feedback":"poor","category":"CC"}, {"date":"2017-12-15T16:53:02.042","name":"xyz","feedback":"very bad","category":"AA"}, {"date":"2017-12-15T16:53:02.042","name":"xyz","feedback":"nice","category":"XX"}, {"date":"2017-12-15T16:53:02.042","name":"xyz","feedback":"okey","category":"YY"} ]; 
 

 
var result = Object.values(data.reduce((r,o) => { 
 
    var key = o.data + "|" + o.name; 
 
    r[key] = r[key] || {date: o.date, name: o.name, count:1}; 
 
    r[key] = Object.assign({}, r[key], {['feedback_'+r[key].count] : o.feedback, ['category_'+r[key].count]: o.category, count : r[key].count+1}); 
 
    return r; 
 
},{})); 
 

 
result.forEach(o => delete o.count); 
 

 
console.log(result);

1

あなたはlinq.jsライブラリを試すことができます。

var data = [{"date":"2017-11-20T17:12:01.340","name":"abc","feedback":"good","category":"AA"}, {"date":"2017-11-20T17:12:01.340","name":"abc","feedback":"bad","category":"BB"}, {"date":"2017-11-20T17:12:01.340","name":"abc","feedback":"poor","category":"CC"}, {"date":"2017-12-15T16:53:02.042","name":"xyz","feedback":"very bad","category":"AA"}, {"date":"2017-12-15T16:53:02.042","name":"xyz","feedback":"nice","category":"XX"}, {"date":"2017-12-15T16:53:02.042","name":"xyz","feedback":"okey","category":"YY"}]; 
 

 
var temp = Enumerable.From(data).GroupBy("$.date").Select("{date: $.Key(), name: $.First().name, feedback: Enumerable.From($).Select('$.feedback').ToArray(), category: Enumerable.From($).Select('$.category').ToArray() }").ToArray(); 
 

 
for (var item of temp) { 
 
    for (var prop of ['feedback', 'category']) { 
 
    for (var x of item[prop]) 
 
     item[prop + '_' + (item[prop].indexOf(x) + 1)] = x 
 
    delete item[prop]; 
 
    } 
 
} 
 

 
console.log(temp);
<script src="https://cdnjs.cloudflare.com/ajax/libs/linq.js/2.2.0.2/linq.min.js"></script>

関連する問題