2016-08-05 73 views
0

下面是我的JSON對象,我想從中刪除groupType,因爲它沒有trip.ie。,trips.length===0。並且如果全部刪除整個客戶端對象車次數組爲空爲該特定客戶對象..根據條件刪除嵌套JSON中的元素

let fruitsArray= [ 
     { 
      "fruit": { 
       "id": 1, 
       "name": "Mango" 
      }, 
      "group": [ 
       { 
        "groupType": { 
         "id": 1, 
         "name": "A" 
        }, 
        "trips": [] 
       }, 
       { 
        "groupType": { 
         "id": 2, 
         "name": "B" 
        }, 
        "trips": [] 
       }, 
       { 
        "groupType": { 
         "id": 3, 
         "name": "C" 
        }, 
        "trips": [] 
       }, 
       { 
        "groupType": { 
         "id": 4, 
         "name": "D" 
        }, 
        "trips": [] 
       } 
      ] 
     }, 
     { 
      "fruit": { 
       "id": 1, 
       "name": "Apple" 
      }, 
      "group": [ 
       { 
        "groupType": { 
         "id": 1, 
         "name": "A" 
        }, 
        "trips": [] 
       }, 
       { 
        "groupType": { 
         "id": 2, 
         "name": "B" 
        }, 
        "trips": [ 
         { 
          "trip": { 
           "id": 62, 
           "deliverySlotId": 900000000000001 
          } 
         } 
        ] 
       }, 
       { 
        "groupType": { 
         "id": 3, 
         "name": "C" 
        }, 
        "trips": [] 
       }, 
       { 
        "groupType": { 
         "id": 4, 
         "name": "D" 
        }, 
        "trips": [] 
       } 
      ] 
     }, 
     { 
      "fruit": { 
       "id": 95, 
       "name": "Banana" 
      }, 
      "group": [ 
       { 
        "groupType": { 
         "id": 4, 
         "name": "A1" 
        }, 
        "trips": [ 
         { 
          "trip": { 
           "id": 62, 
           "deliverySlotId": 900000000000001 
          } 
         }, 
         { 
          "trip": { 
           "id": 63, 
           "deliverySlotId": 900000000000001 
          } 
         }, 
         { 
          "trip": { 
           "id": 65, 
           "deliverySlotId": 900000000000001, 
           "orderId": 22 
          } 
         } 
        ] 
       } 
      ] 
     } 
    ] 

,我試圖返回0 groupType所有元素的代碼..

let finalArray = fruitsArray.map((group) => { 
    return group.group.filter((trip) => { 
     return trip.trips.length > 0; 
    })      
}) 
+0

你也想如果沒有在裏面游來刪除整個水果? – JuHwon

+0

是@ JuHwon.The整個水果 – forgottofly

+0

而不是地圖,你可以使用減少和有條件地推動對象,如果該組沒有旅行 'let finalArray = fruitsAray.reduce((res,group)=> { return res.concat (group.group.filter((trip)=> { return trip.trips.length> 0; })|| []); },[])' 我還沒有測試過代碼。我希望你明白這個主意。 – z0mBi3

回答

1

可以使用map與組合。 或者,您也可以使用reduce方法。

我在下面寫了兩個例子。

let fruitsArray= [ 
 
     { 
 
      "fruit": { 
 
       "id": 1, 
 
       "name": "Mango" 
 
      }, 
 
      "group": [ 
 
       { 
 
        "groupType": { 
 
         "id": 1, 
 
         "name": "A" 
 
        }, 
 
        "trips": [] 
 
       }, 
 
       { 
 
        "groupType": { 
 
         "id": 2, 
 
         "name": "B" 
 
        }, 
 
        "trips": [] 
 
       }, 
 
       { 
 
        "groupType": { 
 
         "id": 3, 
 
         "name": "C" 
 
        }, 
 
        "trips": [] 
 
       }, 
 
       { 
 
        "groupType": { 
 
         "id": 4, 
 
         "name": "D" 
 
        }, 
 
        "trips": [] 
 
       } 
 
      ] 
 
     }, 
 
     { 
 
      "fruit": { 
 
       "id": 1, 
 
       "name": "Apple" 
 
      }, 
 
      "group": [ 
 
       { 
 
        "groupType": { 
 
         "id": 1, 
 
         "name": "A" 
 
        }, 
 
        "trips": [] 
 
       }, 
 
       { 
 
        "groupType": { 
 
         "id": 2, 
 
         "name": "B" 
 
        }, 
 
        "trips": [ 
 
         { 
 
          "trip": { 
 
           "id": 62, 
 
           "deliverySlotId": 900000000000001 
 
          } 
 
         } 
 
        ] 
 
       }, 
 
       { 
 
        "groupType": { 
 
         "id": 3, 
 
         "name": "C" 
 
        }, 
 
        "trips": [] 
 
       }, 
 
       { 
 
        "groupType": { 
 
         "id": 4, 
 
         "name": "D" 
 
        }, 
 
        "trips": [] 
 
       } 
 
      ] 
 
     }, 
 
     { 
 
      "fruit": { 
 
       "id": 95, 
 
       "name": "Banana" 
 
      }, 
 
      "group": [ 
 
       { 
 
        "groupType": { 
 
         "id": 4, 
 
         "name": "A1" 
 
        }, 
 
        "trips": [ 
 
         { 
 
          "trip": { 
 
           "id": 62, 
 
           "deliverySlotId": 900000000000001 
 
          } 
 
         }, 
 
         { 
 
          "trip": { 
 
           "id": 63, 
 
           "deliverySlotId": 900000000000001 
 
          } 
 
         }, 
 
         { 
 
          "trip": { 
 
           "id": 65, 
 
           "deliverySlotId": 900000000000001, 
 
           "orderId": 22 
 
          } 
 
         } 
 
        ] 
 
       } 
 
      ] 
 
     } 
 
    ]; 
 

 

 
var filteredFruits = fruitsArray.map((fruit) => { 
 
    fruit.group = fruit.group.filter((group) => group.trips.length); 
 
    return fruit; 
 
}).filter((fruit) => fruit.group.length); 
 

 
console.log(filteredFruits); 
 

 
// alternative example with reduce 
 
var reducedFruits = fruitsArray.reduce((prev, next) => { 
 
    next.group = next.group.filter((group) => group.trips.length); 
 
    return next.group.length ? prev.concat([next]) : prev; 
 
}, []); 
 

 
console.log(reducedFruits);

1
var fruitsArray= [ 
     { 
      "fruit": { 
       "id": 1, 
       "name": "Mango" 
      }, 
      "group": [ 
       { 
        "groupType": { 
         "id": 1, 
         "name": "A" 
        }, 
        "trips": [] 
       }, 
       { 
        "groupType": { 
         "id": 2, 
         "name": "B" 
        }, 
        "trips": [] 
       }, 
       { 
        "groupType": { 
         "id": 3, 
         "name": "C" 
        }, 
        "trips": [] 
       }, 
       { 
        "groupType": { 
         "id": 4, 
         "name": "D" 
        }, 
        "trips": [] 
       } 
      ] 
     }, 
     { 
      "fruit": { 
       "id": 1, 
       "name": "Apple" 
      }, 
      "group": [ 
       { 
        "groupType": { 
         "id": 1, 
         "name": "A" 
        }, 
        "trips": [] 
       }, 
       { 
        "groupType": { 
         "id": 2, 
         "name": "B" 
        }, 
        "trips": [ 
         { 
          "trip": { 
           "id": 62, 
           "deliverySlotId": 900000000000001 
          } 
         } 
        ] 
       }, 
       { 
        "groupType": { 
         "id": 3, 
         "name": "C" 
        }, 
        "trips": [] 
       }, 
       { 
        "groupType": { 
         "id": 4, 
         "name": "D" 
        }, 
        "trips": [] 
       } 
      ] 
     }, 
     { 
      "fruit": { 
       "id": 95, 
       "name": "Banana" 
      }, 
      "group": [ 
       { 
        "groupType": { 
         "id": 4, 
         "name": "A1" 
        }, 
        "trips": [ 
         { 
          "trip": { 
           "id": 62, 
           "deliverySlotId": 900000000000001 
          } 
         }, 
         { 
          "trip": { 
           "id": 63, 
           "deliverySlotId": 900000000000001 
          } 
         }, 
         { 
          "trip": { 
           "id": 65, 
           "deliverySlotId": 900000000000001, 
           "orderId": 22 
          } 
         } 
        ] 
       } 
      ] 
     } 
    ] 


    for(var i=0;i<fruitsArray.length;i++){ 
    var currentFruit = fruitsArray[i]; 
    var currentFruitGroups = currentFruit.group; 
     var foundTrip =false; 

     for(var j=0;j<currentFruitGroups.length;j++){ 

        var currentTrips = currentFruitGroups[j].trips; 

        if(!currentTrips.length){ 

       currentFruitGroups.splice(j,1); 
       j--; 
      }else{ 
       foundTrip = true; 
      } 
       } 
      if(!foundTrip){ 
      fruitsArray.splice(i,1); 
      i--; 
      } 
    } 
    console.log(fruitsArray); 

https://jsfiddle.net/485ue5zs/1/

1
let finalArray = fruitsArray.reduce((p, n) => { 
    const gt = n.group.filter(g => g.trips.length); 
    return gt.length ? p.concat({fruit: n.fruit, group: gt}) : p; 
}, []);