2017-12-19 1 views
-2

私はjsonを初めて使用しています。JSONオブジェクトフィルタ

私は以下のとおりです。

var data =[ 
    { 
     "year":2015, 
     "month":1, 
     "total_sales":123 
    }, 
    { 
     "year":2015, 
     "month":2, 
     "total_sales":212 
    }, 
    { 
     "year":2015, 
     "month":3, 
     "total_sales":602 
    }, 
    { 
     "year":2015, 
     "month":4, 
     "total_sales":918 
    }, 
    { 
     "year":2015, 
     "month":5, 
     "total_sales":920 
    }, 
    { 
     "year":2015, 
     "month":6, 
     "total_sales":167 
    }, 
    { 
     "year":2015, 
     "month":7, 
     "total_sales":922 
    }, 
    { 
     "year":2015, 
     "month":8, 
     "total_sales":162 
    }, 
    { 
     "year":2015, 
     "month":9, 
     "total_sales":678 
    }, 
    { 
     "year":2015, 
     "month":10, 
     "total_sales":312 
    }, 
    { 
     "year":2015, 
     "month":11, 
     "total_sales":895 
    }, 
    { 
     "year":2015, 
     "month":12, 
     "total_sales":470 
    }, 
    { 
     "year":2016, 
     "month":1, 
     "total_sales":962 
    }, 
    { 
     "year":2016, 
     "month":2, 
     "total_sales":501 
    }, 
    { 
     "year":2016, 
     "month":3, 
     "total_sales":115 
    }, 
    { 
     "year":2016, 
     "month":4, 
     "total_sales":193 
    }, 
    { 
     "year":2016, 
     "month":5, 
     "total_sales":199 
    }, 
    { 
     "year":2016, 
     "month":6, 
     "total_sales":169 
    }, 
    { 
     "year":2016, 
     "month":7, 
     "total_sales":482 
    }, 
    { 
     "year":2016, 
     "month":8, 
     "total_sales":197 
    }, 
    { 
     "year":2016, 
     "month":9, 
     "total_sales":375 
    }, 
    { 
     "year":2016, 
     "month":10, 
     "total_sales":497 
    }, 
    { 
     "year":2016, 
     "month":11, 
     "total_sales":692 
    }, 
    { 
     "year":2016, 
     "month":12, 
     "total_sales":868 
    }, 
    { 
     "year":2017, 
     "month":1, 
     "total_sales":292 
    }, 
    { 
     "year":2017, 
     "month":2, 
     "total_sales":602 
    }, 
    { 
     "year":2017, 
     "month":3, 
     "total_sales":744 
    }, 
    { 
     "year":2017, 
     "month":4, 
     "total_sales":248 
    }, 
    { 
     "year":2017, 
     "month":5, 
     "total_sales":549 
    }, 
    { 
     "year":2017, 
     "month":6, 
     "total_sales":694 
    }, 
    { 
     "year":2017, 
     "month":7, 
     "total_sales":793 
    }, 
    { 
     "year":2017, 
     "month":8, 
     "total_sales":436 
    }, 
    { 
     "year":2017, 
     "month":9, 
     "total_sales":752 
    }, 
    { 
     "year":2017, 
     "month":10, 
     "total_sales":852 
    }, 
    { 
     "year":2017, 
     "month":11, 
     "total_sales":588 
    }, 
    { 
     "year":2017, 
     "month":12, 
     "total_sales":807 
    } 
] 

そして私はそれを以下のフォーマットで縮小したいと思います。これまでに試みられてきた何

var data = [ 
    { 
     "month":"january", 
     "2015":123, 
     "2016":962, 
     "2017":292 
    }, 
    { 
     "month":"february", 
     "2015":212, 
     "2016":30, 
     "2017744 602 },{ "  month":"march", 
     "2015":212, 
     "2016":30, 
     "2017":20 
    } .... 
]; 

:事前に

var uniqueYears = []; 
for (i = 0; i < data.length; i++) { 
    if (uniqueYears.indexOf(data[i].year) === -1) { 
     uniqueYears.push(data[i].year); 
    } 
} 
var uniqueMonths = []; 
for (i = 0; i < data.length; i++) { 
    if (uniqueMonths.indexOf(data[i].month) === -1) { 
     uniqueMonths.push(data[i].month); 
    } 
} 
var attributelist = []; 
console.log(data.length); 
for (i = 0; i < data.length; i++) { 
    var as = $(data).filter(function(i, n) { 
     console.log(n) return n.month === uniqueMonths[i] 
    }); 
} 

感謝。

+5

あなたがこれまでにしようとしているものを投稿してください。 –

+0

あなたはStackOverflowがヘルプデスクだと思いますか?スニーカーになろうとはしませんが、もしそれがあなたの考えであれば、それは –

+1

ではありません。あなたが試したことを少しでも努力して見せてください。 – Amy

答えて

0

月ごとにオブジェクトを含む事前定義された配列を使用し、指定されたオブジェクトの配列を反復し、年をキーとして合計値として月の項目に割り当てられた合計値を割り当てることができます。

var array = [{ year: 2015, month: 1, total_sales: 123 }, { year: 2015, month: 2, total_sales: 212 }, { year: 2015, month: 3, total_sales: 602 }, { year: 2015, month: 4, total_sales: 918 }, { year: 2015, month: 5, total_sales: 920 }, { year: 2015, month: 6, total_sales: 167 }, { year: 2015, month: 7, total_sales: 922 }, { year: 2015, month: 8, total_sales: 162 }, { year: 2015, month: 9, total_sales: 678 }, { year: 2015, month: 10, total_sales: 312 }, { year: 2015, month: 11, total_sales: 895 }, { year: 2015, month: 12, total_sales: 470 }, { year: 2016, month: 1, total_sales: 962 }, { year: 2016, month: 2, total_sales: 501 }, { year: 2016, month: 3, total_sales: 115 }, { year: 2016, month: 4, total_sales: 193 }, { year: 2016, month: 5, total_sales: 199 }, { year: 2016, month: 6, total_sales: 169 }, { year: 2016, month: 7, total_sales: 482 }, { year: 2016, month: 8, total_sales: 197 }, { year: 2016, month: 9, total_sales: 375 }, { year: 2016, month: 10, total_sales: 497 }, { year: 2016, month: 11, total_sales: 692 }, { year: 2016, month: 12, total_sales: 868 }, { year: 2017, month: 1, total_sales: 292 }, { year: 2017, month: 2, total_sales: 602 }, { year: 2017, month: 3, total_sales: 744 }, { year: 2017, month: 4, total_sales: 248 }, { year: 2017, month: 5, total_sales: 549 }, { year: 2017, month: 6, total_sales: 694 }, { year: 2017, month: 7, total_sales: 793 }, { year: 2017, month: 8, total_sales: 436 }, { year: 2017, month: 9, total_sales: 752 }, { year: 2017, month: 10, total_sales: 852 }, { year: 2017, month: 11, total_sales: 588 }, { year: 2017, month: 12, total_sales: 807 }], 
 
    grouped = [{ month: 'january' }, { month: 'february' }, { month: 'march' }, { month: 'april' }, { month: 'may' }, { month: 'june' }, { month: 'july' }, { month: 'august' }, { month: 'september' }, { month: 'october' }, { month: 'november' }, { month: 'december' }]; 
 

 
array.forEach(function (o) { 
 
    grouped[o.month - 1][o.year] = o.total_sales; 
 
}); 
 

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