json 按照欄位分類

小花貓Jane發表於2017-12-14

let arr = [

{

Category:'A',

Amount:1,

},{

Category:'B',

Amount:2,

},{

Category:'A',

Amount:3,

},{

Category:'C',

Amount:4,

},{

Category:'B',

Amount:5,

},{

Category:'C',

Amount:6,

},{

Category:'A',

Amount:7,

},{

Category:'C',

Amount:8,

let temp = {};

let end = [];

for(let i = 0; i < arr.length; i++){

let a = arr[i];

if(!temp[a.Category]){

end.push({

Category: a.Category,

data: [a]

});

temp[a.Category] = a;

}else{

for(let j = 0; j < end.length; j++){

let b = end[j];

if(b.Category == a.Category){

b.data.push(a);

break;

console.log(end);

想要學習前端開發的同學,可以加群:
543627393
學習哦!


相關文章